private int mSelectedPos = 0;
List<StartCheckBean> items;
@Override
public void onBindViewHolder(@NonNull StartCheckItmenListAdapter.ViewHolder holder, int position) {
Log.d("TAG", "onBindViewHolder() called with: holder = [" + holder + "], position = [" + position + "]");
holder.checkBox2.setChecked(items.get(position).getSelected());//“CheckBox”
holder.textView102.setText(items.get(position).getName());//TextView
holder.checkBox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//方法一 : 当数量较大时 会卡顿
//实现单选,第一种方法,十分简单, Lv Rv通用,因为它们都有notifyDataSetChanged()方法
// 每次点击时,先将所有的selected设为false,并且将当前点击的item 设为true, 刷新整个视图
// for (StartCheckBean data : items) {
// data.setSelected(false);
// }
// items.get(position).setSelected(true);
// notifyDataSetChanged();
//方法二
if (mSelectedPos!=position){
//先取消上个item的勾选状态
items.get(mSelectedPos).setSelected(false);
notifyItemChanged(mSelectedPos);
//设置新Item的勾选状态
mSelectedPos = position;
items.get(mSelectedPos).setSelected(true);
notifyItemChanged(mSelectedPos);
}
}
});
}
StartCheckBean.class
package com.xy.youxianghaoche.emp;
/**
* @author 杨景
* @description:
* @date : 2021/5/8 0008 10:27
*/
public class StartCheckBean {
private Boolean isSelected;
private String name;
private String checkType;
private int money;
public Boolean getSelected() {
return isSelected;
}
public String getName() {
return name;
}
public String getCheckType() {
return checkType;
}
public int getMoney() {
return money;
}
public void setSelected(Boolean selected) {
isSelected = selected;
}
public void setName(String name) {
this.name = name;
}
public void setCheckType(String checkType) {
this.checkType = checkType;
}
public void setMoney(int money) {
this.money = money;
}
public StartCheckBean(Boolean isSelected, String name, String checkType, int money) {
this.isSelected = isSelected;
this.name = name;
this.checkType = checkType;
this.money = money;
}
}