设置单选的listView或者gridview

主要是这个BeaseAdapter的方法notifyDataSetChanged()的使用;
作用 :调用BaseAdapter中的getView();方法,刷新ListView中的数据。
实现:1.在BaseAdapter里面定义全局变量 int selectIndex;
2.为listView添加setOnItemClickListener()事件:里面改变selectIndex的值==》
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//选中时改变颜色
adapter.selectIndex=position;
adapter.notifyDataSetChanged();
}
});

2.在getView()方法里面加上一个if:
if( selectIndex == position ){
view.setBackgroundColor(Color.parseColor("#82eae8"));
}else{
view.setBackgroundColor(Color.alpha(0));
}
}
原文地址:https://www.cnblogs.com/shoneworn/p/5796338.html