android – 防止适配器在滚动时回收视图(编辑不要这样做.)

前端之家收集整理的这篇文章主要介绍了android – 防止适配器在滚动时回收视图(编辑不要这样做.)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个自定义基本适配器,它将接收数据的arraylist.从这里开始,它将使用自定义按钮填充网格视图.它完美地完成并填满了gridview.问题是.我想设置一个按钮来改变颜色.当我这样做时,由于视图被回收,它还会更改下一个被回收的视图.防爆.单击位置0处的按钮1.同时更改位置13处的按钮.现在,当我进行一些调试时,我发现它也会更改某些属性.我想知道是否有创建我的视图,因为它不需要回收任何部分的视图.

我已经看到了一些关于使用stableID的事情,但即使我已经将它重写为true.它目前仍然没有改变它.

  1. static class CategoryButtonAdapter extends BaseAdapter
  2. {
  3. private Context mContext;
  4. private ArrayList<DishCategory> dishCategories;
  5. private ArrayList<Dish> dishItems;
  6. static ArrayList<DishCategoryButton> mDishCategoryButtons;
  7. //will take in an array list created in the orderlayout that will be the
  8. //dish category. This will be the from where we will the count for the adapter
  9. public CategoryButtonAdapter(Context context,ArrayList<DishCategory> dishCategories)
  10. {
  11. this.mContext = context;
  12. this.dishCategories = dishCategories;
  13.  
  14. dishItems = dishCategories.get(0).getDishes();
  15. }
  16.  
  17. public int getCount()
  18. {
  19. return dishCategories.size();
  20. }
  21.  
  22. //to be implementated later so it can b3e used to find menu categories
  23. @Override
  24. public DishCategory getItem(int position)
  25. {
  26. return dishCategories.get(position);
  27. }
  28.  
  29. public void getDishCategoryButtons()
  30. {
  31. if(mDishCategoryButtons.size() == 0)
  32. {
  33. System.out.println("The number of buttons in this adapapter is " + mDishCategoryButtons.size());
  34. }
  35. else
  36. {
  37. System.out.println("The number of buttons in this adapapter is " + mDishCategoryButtons.size());
  38. }
  39. }
  40.  
  41. public long getItemId(int position)
  42. {
  43. return dishCategories.get(position).getDishCategoryID();
  44. }
  45.  
  46. @Override
  47. public boolean hasStableIds() {
  48. //return super.hasStableIds(); //To change body of generated methods,choose Tools | Templates.
  49. return true;
  50. }
  51.  
  52. public View getView(int position,View convertView,ViewGroup parent)
  53. {
  54. ViewHolder holder;
  55. DishCategoryButton button = null;
  56. //button to be created
  57. if(convertView == null )
  58. {
  59. holder = new ViewHolder();
  60. //if it is not recycled,initialize some new attributes
  61. button = new DishCategoryButton(this.mContext,dishCategories.get(position));
  62. button.setLayoutParams(new GridView.LayoutParams(100,100));
  63. button.setPadding(2,2,2);
  64. //convertView.setTag(holder);
  65. button.setTag(holder);
  66. }
  67. else
  68. {
  69. //holder = (ViewHolder)convertView.getTag();
  70. button = (DishCategoryButton) convertView;
  71. }
  72. //setButton to the description of the category
  73. //mDishCategoryButtons.add(button);
  74. button.setText((dishCategories.get(position).getDescription()));
  75. //this can be changed later to change the sex appeal of the app
  76. //for now it will be plain
  77. button.setId(position);
  78.  
  79. //.setOnClickListener(new View.OnClickListener()
  80. button.setOnClickListener(new View.OnClickListener() {
  81. public void onClick(View v) {
  82. // Perform action on click
  83. DishCategoryButton dishCategoryButton = (DishCategoryButton)v;
  84. PaintDrawable drawable = (PaintDrawable) dishCategoryButton.getBackground();
  85. System.out.println("Dish button position is " + dishCategoryButton.getId());
  86. //System.out.println("The position from the array says it is at " + position);
  87. System.out.println("Dish Category is " + dishCategoryButton.getDishCategory().getDescription());
  88. System.out.println("Is it currently selected " + dishCategoryButton.getIsSelected());
  89.  
  90. int color = drawable.getPaint().getColor();
  91. System.out.println("Color is " + color);
  92. dishCategoryButton.setIsSelected(true);
  93. drawable = (PaintDrawable) dishCategoryButton.getBackground();
  94. color = drawable.getPaint().getColor();
  95. System.out.println("Color is " + color);
  96. System.out.println("hi");
  97.  
  98. // The toggle is enabled
  99.  
  100. }
  101. });
  102. //new loadDishItems(categoryButtons.get(position).getDescription()));
  103. return button;
  104. }

不要担心视图持有者.这是为了防止回收.关于如何获得这个的任何线索或想法?

这是我的按钮

  1. public class DishCategoryButton extends Button
  2. {
  3. private DishCategory dishCategory = new DishCategory();
  4. private Boolean isSelected = false;
  5.  
  6.  
  7. public DishCategoryButton(Context context,DishCategory dishCategory)
  8. {
  9. super(context);
  10. this.dishCategory = dishCategory;
  11. isSelected = false;
  12. setTextColor(Color.WHITE);
  13. setBackgroundDrawable(new PaintDrawable(Color.BLACK));
  14. }
  15. public DishCategory getDishCategory()
  16. {
  17. return dishCategory;
  18. }
  19. public void setDishCategory(DishCategory dishCategory)
  20. {
  21. this.dishCategory = dishCategory;
  22. }
  23.  
  24. public Boolean getIsSelected() {
  25. return isSelected;
  26. }
  27.  
  28. public void setIsSelected(Boolean isSelected) {
  29. this.isSelected = isSelected;
  30. if(isSelected == true)
  31. {
  32. setTextColor(Color.WHITE);
  33. setBackgroundDrawable(new PaintDrawable(Color.GREEN));
  34. }
  35. else
  36. {
  37. setTextColor(Color.WHITE);
  38. setBackgroundDrawable(new PaintDrawable(Color.BLACK));
  39. }
  40. }

}

解决方法

Prevent the adapter from recycling views on scroll

只是不要使用传递给getView()的convertView参数,并始终返回一个新生成的视图.

但是,就性能而言,这是一个糟糕的解决方案.相反,你的目标不应该是防止回收,而是要相应地回收:你的getView()应该将convertView重置为它的原始状态.

因此,如果某些Button的属性从非默认值更改,请将其重置为getView()中的默认值.

猜你在找的Android相关文章