$ P $从滚动循环的观点pvent适配器 [英] Prevent the adapter from recycling views on scroll

查看:115
本文介绍了$ P $从滚动循环的观点pvent适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的基本适配器将在数据的ArrayList。从这里将填写自定义按钮的网格视图。它这样做完全填补了gridview的。问题是。我想设置一个按钮来改变改变颜色。当我这样做,因为该视图被回收,也改变了回收下一个视图。防爆。位置0点击按钮一次也改变了在13位置按钮现在,当我做一些调试,我发现,它也改变了一些属性。我想知道如果有无论如何创建我的看法,因为它是无需回收的意见任何部分。

我已经看到了关于使用stableID的,但即使我已经重写它真正的一些事情。它还是目前不改变它。

 静态类CategoryButtonAdapter延伸BaseAdapter
{
    私人语境mContext;
    私人的ArrayList< D​​ishCategory> dishCategories;
    私人的ArrayList<&盘GT; dishItems;
    静态的ArrayList< D​​ishCategoryButton> mDishCategoryButtons;
    //将在orderlayout创建一个数组列表,这将是
    //菜类别。这将是从那里我们将计数为适配器
    公共CategoryButtonAdapter(上下文的背景下,ArrayList的< D​​ishCategory> dishCategories)
    {
        this.mContext =背景;
        this.dishCategories = dishCategories;        dishItems = dishCategories.get(0).getDishes();
    }    公众诠释getCount将()
    {
        返回dishCategories.size();
    }    //稍后implementated所以它可以b3e用于查找菜单分类
    @覆盖
    公共DishCategory的getItem(INT位置)
    {
        返回dishCategories.get(位置);
    }    公共无效getDishCategoryButtons()
    {
        如果(mDishCategoryButtons.size()== 0)
        {
             的System.out.println(按钮在此adapapter数为+ mDishCategoryButtons.size());
        }
        其他
        {
            的System.out.println(按钮在此adapapter数为+ mDishCategoryButtons.size());
        }
    }    众长getItemId(INT位置)
    {
        返回dishCategories.get(位置).getDishCategoryID();
    }    @覆盖
    公共布尔hasStableIds(){
        //返回super.hasStableIds(); //要更改生成的方法体,选择Tools |模板。
        返回true;
    }    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        ViewHolder持有人;
        DishCategoryButton按钮= NULL;
        //要创建的按钮
        如果(convertView == NULL)
        {
            持有人=新ViewHolder();
            //如果没有回收,初始化一些新的属性
            按钮=新DishCategoryButton(this.mContext,dishCategories.get(位置));
            button.setLayoutParams(新GridView.LayoutParams(100,100));
            button.setPadding(2,2,2,2);
            //convertView.setTag(holder);
            button.setTag(保持器);
        }
        其他
        {
            //支架=(ViewHolder)convertView.getTag();
            按钮=(DishCategoryButton)convertView;
        }
        // SET按钮的类别的描述
        //mDishCategoryButtons.add(button);
        button.setText((dishCategories.get(位置).getDescription()));
        //这个以后可以改改了应用程序的性感
        //现在它是纯
        button.setId(位置);        //.setOnClickListener(new View.OnClickListener()
        button.setOnClickListener(新View.OnClickListener(){
         公共无效的onClick(视图v){
             //执行上的点击动作
            DishCategoryButton dishCategoryButton =(DishCategoryButton)V;
            PaintDrawable绘制=(PaintDrawable)dishCategoryButton.getBackground();
            的System.out.println(碟按钮的位置是+ dishCategoryButton.getId());
            从阵列//System.out.println(\"The位置说,这是在+位置);
            的System.out.println(这道菜类是+ dishCategoryButton.getDishCategory()getDescription());
            的System.out.println(是不是当前选定的+ dishCategoryButton.getIsSelected());            INT颜色= drawable.getPaint()的getColor()。
                    的System.out.println(颜色是+彩色);
                    dishCategoryButton.setIsSelected(真);
                    绘制=(PaintDrawable)dishCategoryButton.getBackground();
                    颜色= drawable.getPaint()的getColor()。
                    的System.out.println(颜色是+彩色);
                        的System.out.println(HI);                    //启用的切换            }
        });
        //新loadDishItems(categoryButtons.get(位置).getDescription()));
        返回按钮;
    }

不要担心的观点持有者。这是企图prevent回收。关于如何得到这个任何线索或想法?

下面是我的按钮

 公共类DishCategoryButton扩展了Button
{
私人DishCategory dishCategory =新DishCategory();
私人布尔isSelected = FALSE;
公共DishCategoryButton(上下文的背景下,DishCategory dishCategory)
{
   超级(上下文);
   this.dishCategory = dishCategory;
   isSelected = FALSE;
   setTextColor(Color.WHITE);
   setBackgroundDrawable(新PaintDrawable(Color.BLACK));
}
公共DishCategory getDishCategory()
{
     返回dishCategory;
}
公共无效setDishCategory(DishCategory dishCategory)
{
    this.dishCategory = dishCategory;
}公共布尔getIsSelected(){
    返回isSelected;
}公共无效setIsSelected(布尔isSelected){
    this.isSelected = isSelected;
    如果(isSelected ==真)
    {
        setTextColor(Color.WHITE);
        setBackgroundDrawable(新PaintDrawable(Color.GREEN));
    }
    其他
    {
        setTextColor(Color.WHITE);
        setBackgroundDrawable(新PaintDrawable(Color.BLACK));
    }
}

}


解决方案

  

prevent,从回收的滚动视图的适配器


只要不使用)传递给 convertView 参数getView(总是返回新生成的查看

然而,这在性能方面不好溶液。相反,你的目标不应该是prevent回收,但回收的 correcltly 的:您的 getView()应该重置 convertView 到它的原始状态。

所以,如果有一个改变你的一些按钮的的属性从他们的非默认值进行了修改,重置他们回到默认值在 getView()

I have a custom base adapter that will take in an arraylist of data. From here it will fill out a grid view with custom buttons. It does so perfectly and fills up the gridview. The problem is. I want to set a button to change colors on change. When I do this, since the view is recycled, it also changes the next view that is recycled. Ex. Click on button one at position 0. Also changes button at position 13. Now when I do some debugging, I find that it also changes some of the properties. I am wondering if there is anyway of creating my view as it is without needing to recycle any part of the views.

I have seen some things about using stableID's but even when I have overridden it to true. It still does not currently change it.

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,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;
    }

Don't worry about the view holder. that was an attempt to prevent the recycling. Any clues or ideas on how to get this?

Here is my 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

Just don't use the convertView param passed to getView() and always return a freshly generated View.

However, this is a bad solution in terms of performance. Instead, your goal should not be to prevent recycling but to recycle correcltly: Your getView() should reset the convertView to it's pristine state.

So, if there's a change that some of your Button's properties are changed from their non-default values, reset them back to defaults in getView().

这篇关于$ P $从滚动循环的观点pvent适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆