简体   繁体   中英

android - Single RadioButton in RecyclerView

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.

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.

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);                    

        });
    }
}

KOTLIN 2021 Tested

No matter with datalist size

Single Selection RecyclerView with only RadioButton without RadioGroup and Its Tested

*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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM