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

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

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

static class CategoryButtonAdapter extends BaseAdapter
{
    private Context mContext;
    private ArrayList<DishCategory> dishCategories;
    private ArrayList<Dish> dishItems;
    static ArrayList<DishCategoryButton> mDishCategoryButtons;
    //will take in an array list created in the orderlayout that will be the 
    //dish category. This will be the from where we will the count for the adapter
    public CategoryButtonAdapter(Context context,ArrayList<DishCategory> dishCategories)
    {
        this.mContext = context;
        this.dishCategories = dishCategories;

        dishItems  = dishCategories.get(0).getDishes();
    }

    public int getCount() 
    {
        return dishCategories.size();
    }

    //to be implementated later so it can b3e used to find menu categories
    @Override
    public DishCategory getItem(int position) 
    {
        return dishCategories.get(position);
    }

    public void getDishCategoryButtons()
    {
        if(mDishCategoryButtons.size() == 0)
        {
             System.out.println("The number of buttons in this adapapter is " + mDishCategoryButtons.size());
        }
        else
        {
            System.out.println("The number of buttons in this adapapter is " + mDishCategoryButtons.size());
        }
    }

    public long getItemId(int position) 
    {
        return dishCategories.get(position).getDishCategoryID();
    }

    @Override
    public boolean hasStableIds() {
        //return super.hasStableIds(); //To change body of generated methods,choose Tools | Templates.
        return true;
    }

    public View getView(int position,View convertView,ViewGroup parent) 
    {
        ViewHolder holder;
        DishCategoryButton button = null;
        //button to be created
        if(convertView == null )
        {
            holder = new ViewHolder();
            //if it is not recycled,initialize some new attributes
            button = new DishCategoryButton(this.mContext,dishCategories.get(position));
            button.setLayoutParams(new GridView.LayoutParams(100,100));
            button.setPadding(2,2,2);
            //convertView.setTag(holder);
            button.setTag(holder);
        }
        else
        {
            //holder = (ViewHolder)convertView.getTag();
            button = (DishCategoryButton) convertView;
        }
        //setButton to the description of the category
        //mDishCategoryButtons.add(button);
        button.setText((dishCategories.get(position).getDescription()));
        //this can be changed later to change the sex appeal of the app
        //for now it will be plain
        button.setId(position);

        //.setOnClickListener(new View.OnClickListener() 
        button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             // Perform action on click
            DishCategoryButton dishCategoryButton = (DishCategoryButton)v;
            PaintDrawable drawable = (PaintDrawable) dishCategoryButton.getBackground();
            System.out.println("Dish button position is " + dishCategoryButton.getId());
            //System.out.println("The position from the array says it is at " + position);
            System.out.println("Dish Category is " + dishCategoryButton.getDishCategory().getDescription());
            System.out.println("Is it currently selected  " + dishCategoryButton.getIsSelected());

            int color = drawable.getPaint().getColor();
                    System.out.println("Color is  " + color);
                    dishCategoryButton.setIsSelected(true);
                    drawable = (PaintDrawable) dishCategoryButton.getBackground();
                    color = drawable.getPaint().getColor();
                    System.out.println("Color is  " + color);
                        System.out.println("hi");

                    // The toggle is enabled

            }
        });
        //new loadDishItems(categoryButtons.get(position).getDescription()));
        return button;
    }

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

这是我的按钮

public class DishCategoryButton extends Button 
{
private DishCategory dishCategory = new DishCategory();
private Boolean isSelected = false;


public DishCategoryButton(Context context,DishCategory dishCategory) 
{
   super(context);
   this.dishCategory = dishCategory;
   isSelected = false;
   setTextColor(Color.WHITE);
   setBackgroundDrawable(new PaintDrawable(Color.BLACK));
}
public DishCategory getDishCategory()
{
     return dishCategory;
}
public void setDishCategory(DishCategory dishCategory)
{
    this.dishCategory = dishCategory;
}

public Boolean getIsSelected() {
    return isSelected;
}

public void setIsSelected(Boolean isSelected) {
    this.isSelected = isSelected;
    if(isSelected == true)
    {
        setTextColor(Color.WHITE);
        setBackgroundDrawable(new PaintDrawable(Color.GREEN));
    }
    else
    {
        setTextColor(Color.WHITE);
        setBackgroundDrawable(new PaintDrawable(Color.BLACK));
    }
}

}

解决方法

Prevent the adapter from recycling views on scroll

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

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

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

猜你在找的Android相关文章