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轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...