先上代码
- bean
public class ListBean {
private String name;
boolean check=false;//是否选中
public ListBean(String name) {
this.name = name;
}
public boolean isCheck() {
return check;
}
public void setCheck(boolean check) {
this.check = check;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- Adapter
public class ListAdpter extends BaseQuickAdapter<ListBean, BaseViewHolder> {
public ListAdpter(int layoutResId, @Nullable List<ListBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(@NonNull BaseViewHolder helper, ListBean item) {
RadioButton button= helper.getView(R.id.button);
if (item.isCheck()){
button.setChecked(true);
}else {
button.setChecked(false);
}
}
}
- Activity
private int p=-1;//用来记录上一个点击条目的下标
adpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//判断当第一次和重复点击当前条目
if (p!=-1&&p!=position){
beans.get(p).setCheck(false);
beans.get(position).setCheck(true);
}else {
beans.get(position).setCheck(true);
}
p=position;
adapter.notifyDataSetChanged();
}
});
代码语言:javascript复制处理思路:
1.给Bean类中添加 状态属性
2.setOnItemClickListener 判断 设置状态,同时去除前一个点击的状态
3.Adapter中使用Bean中的状态设置RadioButton