Skip to content Skip to sidebar Skip to footer

Grouping Switches Inside Recyclerview

I am currently facing difficulty trying to group the switches inside recylerView. I have attached my code below and a snapshot of the screen. As you can see, I can now toogle any

Solution 1:

I suggest that you set a Click listener on all value switches and disable all switches when any switch clicked.

@OverridepublicvoidonBindViewHolder(MyViewHolder holder, int position) {
Itemitem= itemList.get(position);
 holder.name.setText(item.getName());
 holder.value.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View view) {
        for (Item x: itemList){
          //  x.Selected=false; // here you make all of your switches to false
        }
        //itm.Selected = true;// here you make the current switch to on or true
        notifyDataSetChanged();
    }
});
 }

Solution 2:

@OverridepublicvoidonBindViewHolder(MyViewHolder holder, int position) {

    Itemitem= itemList.get(position);

    holder.name.setText(item.getName());
    holder.value.setChecked(item.isValueChecked());

    holder.value.setOnCheckedChangeListener(newOnCheckedChangeListener(
    @OverridepublicvoidonCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {

            for(Item i: itemList){
                i.setValueChecked(false);
            }

            if(buttonView.isChecked()){
                itemList.get(position).setValueChecked(true);
                holder.value.setChecked(true);
            }

            notifyDataSetChanged();

    }

    });
}

Try this solution, this should work...

Checked Change Listener is set on the switch if any switch is turned ON then all the values of the itemList is made false and current value is made True and switch is set ON, finally, notifyDataSetChanged() is called.

If any switch is turned OFF then all the values in the itemList are made False and notifyDataSetChanged() is called so that the switch state gets updated.

  holder.value.setChecked(item.isValueChecked());

This updates the switch state based on the values stored in the itemList.

Post a Comment for "Grouping Switches Inside Recyclerview"