Jes*_*001 6 android gridview button baseadapter
我有一个自定义基本适配器,它将接收数据的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,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;
}
Run Code Online (Sandbox Code Playgroud)
不要担心视图持有者.这是为了防止回收.关于如何获得这个的任何线索或想法?
这是我的按钮
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));
}
}
Run Code Online (Sandbox Code Playgroud)
}
Sou*_*dra 15
一种更好的方法是使用
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;
}
Run Code Online (Sandbox Code Playgroud)
防止适配器在滚动时回收视图
只是不要使用convertView传递给的参数getView()并始终返回一个新生成的View.
但是,就性能而言,这是一个糟糕的解决方案。相反,你的目标不应该是防止回收,但回收correcltly:您getView()应该重新设定convertView到它的原始状态。
因此,如果您Button的某些属性从其非默认值发生更改,请将它们重置回getView().
| 归档时间: |
|
| 查看次数: |
15507 次 |
| 最近记录: |