1- You can create a variable inside Adapter class that can be used to hold the selected position:
private int selectedPosition = -1;// no selection by default
2- Inside onBindViewHolder set Check State:
holder.checkBox.setChecked(selectedPosition == position);
3- Inside setOnCheckedChangeListener you must update position
this.selectedPosition = holder.getAdapterPosition();
4- Refresh adapter
adapter.notifyDatasetChanged();
EDIT
onBindViewHolder will be called for all positions,
So method setChecked will called for all CheckBoxes,
This method have a boolean input.
In above example i write setChecked(selectedPosition == position)
. For more readable i can write:
if(selectedPosition == position){
holder.checkBox.setChecked(true);
}
else{
holder.checkBox.setChecked(false);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…