在项目中,需要写一个效果,即listview中的item被点击后,颜色变灰(初始颜色为黑色),在网上找了许多的帖子,都没有详细的代码解决方法,只是说出了大概的思路,通过Map或者List集合,来记录当前点击的item,但是后面怎么继续,却没有下文,于是只能自己研究。
结合网络上找到的资源,已经通过Map记录了当前点击的item,那么Map<Integer,Boolean>记录方式可以定义为当前点击的position 和当前item的boolean状态,具体代码如下
//声明
private Map<Integer, Boolean> m;
public Myadapter(Context context, Map<Integer, Boolean> map) {
this.mInflater = LayoutInflater.from(context);
this.m = map;
m = new HashMap<Integer, Boolean>();
}
//给Map添加方法,在MainActivity中调用,直接添加数据
public Map<Integer, Boolean> getMap(Integer position, Boolean flag) {
m.put(position, flag);
return m;
}
在GetView方法里面,根据Map中Boolean的值 来设置
public View getView(int position, View convertView, ViewGroup parent