【发布时间】:2011-11-10 01:30:49
【问题描述】:
我的行包含一个按钮,该按钮在我的适配器的 getView 中设置了自己的单击侦听器。我可以在行的父项中使用 android:descendantFocusability="blocksDescendants" 区分我的按钮点击和实际的行项目点击。
当我点击一个按钮时,它会正确设置按钮背景,我的问题是当我滚动列表时,它也为不同的行设置了它。我认为他们在某个地方有观点回收的问题。
这是我的代码:
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.todays_sales_favorite_row, null);
holder.favCatBtn = (Button)convertView.findViewById(R.id.favCatBtn);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.favCatBtn.setTag(position);
holder.favCatBtn.setOnClickListener(this);
return convertView;
}
@Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.d(TAG, "Button row pos click: " + pos);
RelativeLayout rl = (RelativeLayout)v.getParent();
holder.favCatBtn = (Button)rl.getChildAt(0);
holder.favCatBtn.setBackgroundResource(R.drawable.icon_yellow_star_large);
}
因此,如果我单击第 1 行的按钮,按钮背景会发生应有的变化。但是当我向下滚动列表时,其他按钮也会随机设置。然后有时当我向上滚动到位置 1 时,按钮背景会再次恢复到原来的状态。
我在这里缺少什么?我知道我就在那里,这只是我没有做的小事。
【问题讨论】:
-
当您更改为 if(true || convertView == null){... (所以它总是膨胀),那么它可以正常工作吗?不是解决方案,只是想知道问题是否不再出现。
-
是的,这会阻止背景在其他随机行上发生变化。显然不是你说的解决方案,但是它会停止问题,直到我向上滚动并且按钮恢复到原来的背景。
标签: android listview button onclick adapter