jojemapa
jojemapa

Reputation: 903

How to change an element of a RecyclerView from one that is inside of it

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.

enter image description here

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

Answers (2)

Dariush Fathi
Dariush Fathi

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

babadaba
babadaba

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

Related Questions