我在ListAdapter中有名单,我会显示图标和图标的名称。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = mInflater.inflate(R.layout.list_item_icon_text, parent, false);
} else {
view = convertView;
}
AppModel item = getItem(position);
ImageView myicon = (ImageView) view.findViewById(R.id.iconList);
myicon.setImageDrawable(item.getIcon());
((TextView)view.findViewById(R.id.textList)).setText(item.getLabel());
}
return view;
}
现在,我想点击名称并在右侧查看刻度线以表明它已被选中,当再次选择时,刻度线应该是不可见的。到目前为止,我没有尝试将刻度线图标设置为ImageView,因为有时当我点击A时,H也有刻度线。
答案 0 :(得分:0)
由于convertView
,你所说的问题是正常的。要解决问题,你可以这样做:
将boolean selected
属性添加到AppModel
public class AppModel {
//....
//your own attributes
private boolean selected;
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
将tick ImageView
添加到您的布局中。当膨胀项目视图时,使用boolean
selected
来控制是否显示勾号。
AppModel item = getItem(position);
ImageView tick = (ImageView) view.findViewById(R.id.tick);
tick.setVisibility(item.isSelected()?View.VISIBLE:View.GONE);
将OnClickListener
添加到您的视图中:
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
item.setSelected(!item.isSelected());
notifyDataSetChanged();
}
});