如题,因为需要实现到listView的单选效果,所以网上搜罗了一大堆别人的实现方式,原理挺简单,但是实现起来挺麻烦的(至少我是这么觉得)。
所以,我分享一个简单方法:
1 . 设置一个全局变量
public static int checkPosition = 0;
或者 本地存储
SharedPreferencesUtils.setParam(context, "checkPosition", position);
2 . 设置一个ImagView iv_check来表示选中的按钮,获取选中的position,改变相应图片
if (position == (int)SharedPreferencesUtils.getParam(context,"checkPosition",0)){
holder.iv_check.setImageResource(R.mipmap.cb_checked);
}else {
holder.iv_check.setImageResource(R.mipmap.cb_unchecked);
}
3 . 点击item的事件里处理
SharedPreferencesUtils.setParam(context, "checkUserPosition", position);
notifyDataSetChanged();//刷新列表
over…