I have a recyclerview in which every item has 3 radiobuttons grouped in a radiogroup. Now a user can select only one radiobutton per item in recyclerview. But I want the user to select only one radiobutton throughout the recyclerview. How can this be achieved?
This is how it looks currently.
I would like to make it possible to check only 1 radiobutton throughout the recycler view. If 1st radio button in first item is checked and after that the user clicks on the 2nd radiobutton in 2nd item, then the 1st radiobutton in the 1st item should get unchecked.
Open “res/layout/main. xml” file, add “RadioGroup“, “RadioButton” and a button, inside the LinearLayout . Radio button selected by default. To make a radio button is selected by default, put android:checked="true" within the RadioButton element.
You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.
RadioButton is a two states button which is either checked or unchecked. If a single radio button is unchecked, we can click it to make checked radio button. Once a radio button is checked, it cannot be marked as unchecked by user. RadioButton is generally used with RadioGroup.
I used this approach
public class OffersRecyclerViewAdapter extends RecyclerView.Adapter<OffersRecyclerViewAdapter.ViewHolder> {
private List<OffersModel> offersList;
private Context context;
private int lastSelectedPosition = -1;
public OffersRecyclerViewAdapter(List<OffersModel> offersListIn, Context ctx) {
offersList = offersListIn;
context = ctx;
}
@Override
public OffersRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.offer_item, parent, false);
OffersRecyclerViewAdapter.ViewHolder viewHolder =
new OffersRecyclerViewAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(OffersRecyclerViewAdapter.ViewHolder holder, int position) {
//since only one radio button is allowed to be selected,
// this condition un-checks previous selections
holder.selectionState.setChecked(lastSelectedPosition == position);
}
@Override
public int getItemCount() {
return offersList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public RadioButton selectionState;
public ViewHolder(View view) {
super(view);
selectionState = (RadioButton) view.findViewById(R.id.offer_select);
selectionState.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastSelectedPosition = getAdapterPosition();
notifyDataSetChanged();
}
});
}
}
}
KOTLIN 2021
Declare a RadioButton in a variable In your Adapter class
private var lastCheckedRB: RadioButton? = null
Now In your Adapter class onBindViewHolder, Write OnclickListener to RadioButton
holder.YOURRADIOBUTTON.setOnClickListener {
if (lastCheckedRB!=null){
lastCheckedRB?.isChecked=false
}
lastCheckedRB = holder.YOURRADIOBUTTON
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With