Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select only one radiobutton in a recyclerview

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.

enter image description here

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.

like image 731
suku Avatar asked Feb 05 '16 07:02

suku


People also ask

How to select one option in radio button in android Studio?

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.

How to group radio buttons?

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.

How to use RadioButton?

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.


2 Answers

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

                }
            });
        }
    }
}
like image 114
Developine Avatar answered Sep 28 '22 19:09

Developine


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
    }
like image 42
G Anil Reddy Avatar answered Sep 28 '22 19:09

G Anil Reddy