1.多选
添加数据
list = new ArrayList<>();
for (int i = 0; i < 8; i++) {
ListDataId listDataId = new ListDataId();
listDataId.setCheck(false);
listDataId.setId(i);
list.add(listDataId);
}
ListDataId:
public class ListDataId {
private boolean isCheck=false;
private int id;
public ListDataId() {
}
@Override
public String toString() {
return "ListDataId{" +
"isCheck=" + isCheck +
", id=" + id +
'}';
}
public boolean isCheck() {
return isCheck;
}
public void setCheck(boolean check) {
isCheck = check;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
3.BaseViewHolder视图方法中的操作
classPreparationRecyclerView.setAdapter(adapter = new BaseQuickAdapter<ListDataId, BaseViewHolder>(R.layout.item_ready_to_work, list) {
@Override
protected void convert(final BaseViewHolder helper, ListDataId item) {
final ListDataId datainfo = list.get(item.getId());
ImageView iv_gouxuan = helper.itemView.findViewById(R.id.iv_gouxuan);
final boolean isCheck = datainfo.isCheck();
if (isCheck) {
//被选
iv_gouxuan.setBackgroundResource(R.drawable.select_yes);
} else {
//未被选
iv_gouxuan.setBackgroundResource(R.drawable.select_no);
}
iv_gouxuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String newId = datainfo.getId() + "";
for (ListDataId datainfo1 : list) {
String oldId = datainfo1.getId() + "";
if (newId.equals(oldId)) {
boolean isCheck1 = datainfo1.isCheck();
if (isCheck1) {
datainfo1.setCheck(false);
} else {
datainfo1.setCheck(true);
}
break;
}
}
//刷新数据
adapter.notifyDataSetChanged();
}
});
}
});
单选并互斥
private int mLeftposition = -1;
渲染视图中HodlerView
//判断传入的position是否和当前一致 int typeID = Integer.parseInt(item); if (typeID == mposition) { lin_item_teacher_bg.setBackgroundResource(R.drawable.border_slid_pink_white_2); } else { lin_item_teacher_bg.setBackgroundResource(R.drawable.border_four_gray_e_white_2); }
item的点击事件中
//将点击的位置传出去 mposition = position; //刷新界面 notify 通知Data 数据set设置Changed变化 //在这里运行notifyDataSetChanged 会导致下面的onBindViewHolder 重新加载一遍 adapter.notifyDataSetChanged();