Select only one radiobutton in a recyclerview

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

                }
            });
        }
    }
}

Here is what worked for me:

 private RadioButton lastCheckedRB = null;
 ...
 @Override
 public void onBindViewHolder(final CoachListViewHolder holder, final int position) {
  View.OnClickListener rbClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton checked_rb = (RadioButton) v;
            if(lastCheckedRB != null){
                lastCheckedRB.setChecked(false);
            }
            lastCheckedRB = checked_rb;
        }
    };

    //price_1m, price3m, price_6m are RadioButtons inside a radiogroup
    holder.price1m.setOnClickListener(rbClick);
    holder.price3m.setOnClickListener(rbClick);
    holder.price6m.setOnClickListener(rbClick);

Here, the radio button is stored temporarily in lastCheckedRB. When a new radio button is clicked, the old radiobutton is unchecked. Initially the lastCheckedRB is set to null.


Here is another method to do the same. This is more elegant than my previous answer. But I have kept both as the previous answer provides more flexibility.

private RadioButton lastCheckedRB = null;
...
@Override
public void onBindViewHolder(final CoachListViewHolder holder, final int position) {
   holder.priceRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);
            if (lastCheckedRB != null) {
                lastCheckedRB.setChecked(false);
            }
            //store the clicked radiobutton
            lastCheckedRB = checked_rb;
        }
    });