android - 防止适配器回收 ScrollView (Edit 永远不会这样做。)

标签 android button gridview baseadapter

我有一个自定义基础适配器,它将接收数据数组列表。从这里开始,它将使用自定义按钮填充 GridView 。它做得非常完美并填满了 GridView 。问题是。我想设置一个按钮来改变颜色。当我这样做时,由于 View 被回收,它也会更改下一个被回收的 View 。前任。单击位置 0 的按钮 1。同时更改位置 13 的按钮。现在,当我进行一些调试时,我发现它也更改了一些属性。我想知道是否可以按原样创建我的 View 而无需回收 View 的任何部分。

我已经看到了一些关于使用 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,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;
    }

不用担心 View 持有者。那是试图阻止回收。有关如何获得此信息的任何线索或想法?

这是我的按钮

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

最佳答案

更好的方法是使用

recyclerView.getRecycledViewPool().setMaxRecycledViews(VIEW_TYPE,0);

您必须注意,这可能会降低 RecyclerView 的性能。

您可以重写 getItemViewType 方法,如下所述

@Override
public int getItemViewType(int position) {
    if (position == feedElements.size())
        return 3;
    else if (feedElements.get(position).getType() == 1)
        return 1;
    else
        return 2;
}

关于android - 防止适配器回收 ScrollView (Edit 永远不会这样做。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721752/

相关文章:

c# - 在 Asp.Net Gridview 中分组数据

javascript - 访问子gridview时如何获取父gridview某一单元格的值?

android - .Override 不同类的方法

android - Google TV 是否需要 WakeLocks 之类的东西?

Android扩展Application模式疑惑

java - Android 自定义按钮字体

android - 如何创建一个包含多个 View 的按钮?

android - 仅发布 7 英寸和 10 英寸平板电脑的 Android 应用程序

javascript - Javascript 可以帮我按 Enter 键吗?

asp.net - ASP :GridView GridLines ="None" 的样式