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 switch but at a given point in time , I want only one switch to be toggled on.
Item_list.xml :-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingBottom="@dimen/row_padding_vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/row_padding_vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="87dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:layout_marginRight="30dp"
card_view:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="95dp"
android:background="#20b2aa"
android:orientation="vertical">
<LinearLayout
android:layout_width="182dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:orientation="horizontal">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff"
android:textSize="32dp" />
</LinearLayout>
<Switch
android:id="@+id/value"
android:layout_width="59dp"
android:layout_height="3dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="-35dp"
android:layout_marginBottom="100dp"
android:scaleX="1.1"
android:scaleY="1.1"
android:theme="@style/SwitchTheme" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</RelativeLayout>
ItemAdapter:-
package com.example.loginpage;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Switch;
import android.widget.TextView;
import java.util.List;
public class ItemAdapter extends
RecyclerView.Adapter<ItemAdapter.MyViewHolder> {
private int cn=0;
private List<Item> itemList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public Switch value;
public MyViewHolder(View view) {
super(view)
name = (TextView) view.findViewById(R.id.name);
value = (Switch) view.findViewById(R.id.value);
}
}
public ItemAdapter(List<Item> itemList) {
this.itemList = itemList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Item item = itemList.get(position);
holder.name.setText(item.getName());
}
@Override
public int getItemCount() {
return itemList.size();
}
}
My current interface looks like this.... https://www.pastepic.xyz/image/8z5Eh
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.
This updates the switch state based on the values stored in the itemList.
I suggest that you set a Click listener on all value switches and disable all switches when any switch clicked.