【发布时间】:2015-10-04 02:13:49
【问题描述】:
在列表视图中我在使用checkboxes要选择列表列表项时,当checkbox是listview项目的背景应该改变,但是当我点击checkbox的第一个项目时,它的背景变化但是当我向下滚动列表项 3 的背景时,checkbox 状态也会发生变化。我不明白为什么会发生这种情况可能是由于 listview 的回收过程。
public View getView(final int position, View convertView, ViewGroup parent) {
TextView tvTitle, tvCat, tvDate;
CheckBox cbAddSummary, cbAddPhoto,cbItemSelect;
Log.i("mponeList:position",String.valueOf(position));
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_mp_one,parent,false);
tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
tvCat = (TextView) convertView.findViewById(R.id.tv_cat);
tvDate = (TextView)convertView.findViewById(R.id.tv_date);
cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary);
cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo);
cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select);
}
tvTitle = (TextView)convertView.findViewById(R.id.tv_title);
tvCat = (TextView) convertView.findViewById(R.id.tv_cat);
tvDate = (TextView)convertView.findViewById(R.id.tv_date);
cbAddSummary = (CheckBox) convertView.findViewById(R.id.cb_add_summary);
cbAddPhoto = (CheckBox) convertView.findViewById(R.id.cb_add_photo);
cbItemSelect = (CheckBox) convertView.findViewById(R.id.cb_item_select);
cbItemSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
View row = ((View) buttonView.getParent());
if (isChecked) {
row.setBackgroundResource(R.drawable.shape_mp_list_item_check);
} else {
row.setBackgroundResource(R.drawable.shape_mm_er_list);
}
}
});
MpOneListModel model = this.list.get(position);
tvTitle.setText(model.getTitle());
tvCat.setText(model.getCat());
tvDate.setText(model.getDate());
return convertView;
}
这是我在适配器的getview 中使用的代码。
【问题讨论】:
-
是的,这正是因为每当你滚动时都会循环到 Listview .....
-
@koutuk 如何避免这种情况
-
这是由于回收到 Listview - 如何避免 --> 通过阅读 ListView 回收?
标签: android listview checkbox android-listview getview