如果我选择了另一个以前选中的复选框必须取消选中,则我想在recyclearview中只允许选中一个复选框,如果我在下面编写了代码,则可以帮助我完成此操作....?
public class RecyMyadptr extends
RecyclerView.Adapter<RecyMyadptr.RecyMyHolder> {
ArrayList<Data> dataaaa=new ArrayList<>();
Context context1;
int i=0;
static int selectedPosition = -1;
RecyMyadptr(Context context , ArrayList<Data> data)
{
this.dataaaa=data;
this.context1=context;
}
@NonNull
@Override
public RecyMyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card, parent, false);
return new RecyMyHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final RecyMyHolder holder, final int position) {
final Data date= dataaaa.get(position);
holder.textView2.setText(date.getName());
holder.textView.setText(date.getId());
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(date.isSelected());
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
date.setSelected(b);
}
});
}
@Override
public int getItemCount() {
return dataaaa.size();
}
class RecyMyHolder extends RecyclerView.ViewHolder
{
TextView textView,textView2;
CheckBox checkBox;
public RecyMyHolder(final View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.text1id);
textView2=itemView.findViewById(R.id.text2id);
checkBox=itemView.findViewById(R.id.checkboxid);
}
}
}