android - Single RadioButton in RecyclerView

10,066

Solution 1

You can manage it by creating a model class with a boolean variable isChecked.

when you select a RadioButton first do isChecked = false for all and then make true for your selected button and then call notifyDataSetChanged.

In adapter use

radioButton.setChecked(list.get(position).isChecked())

It will help you definitely.

Solution 2

I had the same problem. Following the answer of Vishal Chhodwani I wrote the following solution, hoping it can help other readers.

class MyRecycler extends RecyclerView.Adapter<MyRecycler.RecyclerViewHolder> {

    static class RecyclerViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.aRadioButton)
        RadioButton rb;

        public RecyclerViewHolder(View itemView, int viewType, final Context context) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        } 
    }

    private List<MyModel> list;
    private RadioButton selectedRadioButton;  

    // Constructor and other methods here...

    @Override
    public void onBindViewHolder(final MyRecycler.RecyclerViewHolder holder, int position) {

        RadioButton radioButton = holder.rb;
        radioButton.setChecked(list.get(position).isChecked());

        radioButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // Set unchecked all other elements in the list, so to display only one selected radio button at a time 
                for(MyModel model: list)
                    model.setChecked(false);

                // Set "checked" the model associated to the clicked radio button
                list.get(position).setChecked(true);

                // If current view (RadioButton) differs from previous selected radio button, then uncheck selectedRadioButton
                if(null != selectedRadioButton && !v.equals(selectedRadioButton))
                    selectedRadioButton.setChecked(false);

                // Replace the previous selected radio button with the current (clicked) one, and "check" it
                selectedRadioButton = (RadioButton) v;
                selectedRadioButton.setChecked(true);                    

        });
    }
}
Share:
10,066
Den
Author by

Den

BY DAY: Working as a Junior Android Developer :) Playing guitar with my band; Roller-skating; Playing video games. BY NIGHT: Sleeping :) Playing video-games ofc!

Updated on June 23, 2022

Comments

  • Den
    Den almost 2 years

    I have a RecyclerView with item that holds RadioButton only, my adapter creates RecyclerView with some positions - can be 5-10 positions with RadioButton in every position. But these RadioButtons are not in the same RadioGroup because they are all in different RecyclerView positions. Is there any way to set single selection for it?

    PS: I found this Select only one radiobutton in a recyclerview, but it's all about RadioGroups in RecyclerView positions, I have RadioButtons only.

  • Sanjay Kumar
    Sanjay Kumar almost 7 years
    what if we have more than 100 or 1000 items in list.
  • Den
    Den almost 7 years
    Yeah, I got it but my RecyclerView constructed from ArrayList with defined ObjectModels in it.
  • Vishal Chhodwani
    Vishal Chhodwani almost 7 years
    @SanjayMajoka there will not be any problem if it has more than 100 items. this is the easiest way you can do. there is another way that is hold the previous view and unchecked it before and check the new one
  • Vishal Chhodwani
    Vishal Chhodwani almost 7 years
    @Den then take a new variable inside ObjectModels and manage it with this.
  • Vishal Chhodwani
    Vishal Chhodwani almost 7 years
    @Den Use this reference it will help you more stackoverflow.com/a/36930230/6676466 In this it is managing it by holding a previous view of radioButton.
  • Den
    Den almost 7 years
    Thank you, looks like I did it :)