Select only one radiobutton in a recyclerview

主宰稳场 提交于 2019-11-27 00:58:33

问题


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.


回答1:


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



回答2:


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.




回答3:


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

                }
            });
        }
    }
}



回答4:


I faced issues with the accepted answer. Sometimes it makes currently checked radio button unchecked leaving non of the radio buttons selected. This was happening when selecting a radio button in different radio group after selecting a radio button from one group.

Here is the solution, instead of saving last checked RadioButton, save last checked RadioGroup and clear last check if last checked RadioGroup is not the current one and it has a selected RadioButton as shown below.

For more information read http://www.zoftino.com/android-recyclerview-radiogroup

   priceGroup = (RadioGroup) view.findViewById(R.id.price_grp);

    priceGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {

            if (lastCheckedRadioGroup != null
                    && lastCheckedRadioGroup.getCheckedRadioButtonId()
                                     != radioGroup.getCheckedRadioButtonId()
                    && lastCheckedRadioGroup.getCheckedRadioButtonId() != -1) {
                lastCheckedRadioGroup.clearCheck();

                Toast.makeText(PackageRecyclerViewAdapter.this.context,
                        "Radio button clicked " + radioGroup.getCheckedRadioButtonId(),
                        Toast.LENGTH_SHORT).show();

            }
            lastCheckedRadioGroup = radioGroup;

        }
    });



回答5:


Try something similar:

public void onCheckedChanged(RadioGroup radioGroup, int checkedItemId) {
  mCheckedId = checkedItemId;
  mSelectedPosition = getAdapterPosition();
}

The code above is the listener you register to the RadioGroup inside onCreateViewHolder(). The prefixed items are your fields in the adapter.

In onBindViewHolder():

if (position == mSelectedPosition) {
  holder.radioGroup.check(mCheckedId);
} else {
  holder.radioGroup.clearCheck();
}

It should work this way.




回答6:


final String[] country_selcted = new String[1];
    holder.rb_team_one.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        if(b==true)
                            country_selcted[0] =holder.rb_team_one.getText().toString();
                        else
                            country_selcted[0] =holder.rb_team_two.getText().toString();
                    }
                });

Now Print country_selcted[0] it gives you to row radio group selection result only



来源:https://stackoverflow.com/questions/35218633/select-only-one-radiobutton-in-a-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!