Reputation: 903
I have a RecyclerView where I want to create something like the image below
In short: I want to disable a checked checkbox is activated, when another of the list is activated.
This my RecyclerView Adapter:
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
/*Here I want to disable this on. I need to have a single enabled checkbox.*/
//((CheckBox) holder.itemView.findViewById( )).setChecked(false);
}
});
}
As I can disable a CheckBox Checked this on. Sorry for my English...
Upvotes: 0
Views: 107
Reputation: 1695
in coustructor method add this int variable :
int currentCheckedPosition = -1 ; // hold checked position
And in OnBindView() do this :
@Override
public void onBindViewHolder(final MyHolder holder, final int position) {
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
currentCheckedPosition = holder.getAdapterPosition();
holder.checkbox.setChecked(b);
} else {
currentCheckedPosition = - 1 ;
}
notifyDataSetChanged();
}
});
holder.checkBox.setChecked(position == currentCheckedPosition);
}
Upvotes: 1
Reputation: 814
Not the most elegant solution but you could make a CompoundButton variable called currentlyChecked for the whole adapter. When onCheckedChange is fired, call
currentlyChecked.setChecked(false);
Hope this helps
Upvotes: 0