我有一个带有textview和imageview的自定义列表视图,以及像checkmark一样的imageview。在选择textview时,在Listview的OnItemClickListener上,我将使imageview Visibilty可见,以显示已选中。
stickyList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
stickyList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String tag = (String) ((TextView) view
.findViewById(R.id.tvtag)).getText();
ImageView cb = (ImageView)
view.findViewById(R.id.ic_check);
if (cb.getVisibility() == View.VISIBLE) {
cb.setVisibility(
View.GONE);
selectedtags.remove(tag);
} else {
cb.setVisibility(
View.VISIBLE);
selectedtags.add(tag);
}
}
});
它工作正常,直到Listview滚动。如果我选择了listview的前两个postions textview,当我向下滚动listview时,imageview(cb)的可见性对于前两个位置是可见的。这意味着在滚动后,imageview在相同位置可见。
GetviewMethod:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.recenttag_list_item,
parent, false);
holder.text = (TextView) convertView.findViewById(R.id.tvtag);
holder.checkmark = (ImageView) convertView
.findViewById(R.id.ic_check);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(data.get(position).mytag);
return convertView;
}
答案 0 :(得分:4)
您看到的原因是因为列表项视图被回收(这是convertView
的用途)。当您滚动ListView
时,滚动屏幕的上一个项目将作为下一个位置的convertView
传回适配器。
您需要确保在每次调用getView()
时完全重置项目视图的状态,以便这样的更改不会从一次使用视图项目泄漏到下一次。这可能意味着您必须始终检查getView()
中该位置的当前“已检查”值,以确定它是否应该可见。
编辑:也许这会有所帮助...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
/* Unchanged code omitted */
holder.text.setText(data.get(position).mytag);
boolean checkVisible = ???; //Use whatever logic your app has to determine if this position should be checked
holder.checkmark.setVisibility(checkVisible); //Always set the visibility to what it should be for this position
return convertView;
}