android 在回收视图中只选择一个单选按钮

siotufzp  于 2023-02-06  发布在  Android
关注(0)|答案(9)|浏览(169)

我有一个recyclerview,其中每个项目有3个单选按钮组成一个radiogroup。现在一个用户只能选择一个单选按钮在recyclerview中的每个项目。但我希望用户只能选择一个单选按钮在整个recyclerview。这是如何实现的?
这是它现在的样子。

我想使它有可能在整个回收视图中只选中1个单选按钮。如果选中了第一个项目中的第一个单选按钮,然后用户点击了第二个项目中的第二个单选按钮,那么第一个项目中的第一个单选按钮应该取消选中。

aiazj4mn

aiazj4mn1#

这里有另一种方法来做同样的事情。这比我以前的答案更优雅。但我保留了两者,因为以前的答案提供了更多的灵活性。

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

yftpprvb2#

以下是对我起作用的方法:

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

在这里,单选按钮暂时存储在lastCheckedRB中。当单击新单选按钮时,旧单选按钮将取消选中。最初,lastCheckedRB设置为null。

oxiaedzo

oxiaedzo3#

我用这种方法

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

                }
            });
        }
    }
}
yeotifhr

yeotifhr4#

我遇到了接受答案的问题。有时它会使当前选中的单选按钮取消选中,而不选中任何单选按钮。这种情况发生在从一个组中选择单选按钮后,在不同的单选按钮组中选择单选按钮时。
以下是解决方案,而不是保存最后选中的单选按钮,保存最后选中的单选组,并清除最后检查,如果最后选中的单选组不是当前的一个,它有一个选定的单选按钮,如下所示。
有关详细信息,请阅读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;

        }
    });
5cnsuln7

5cnsuln75#

在Kotlin

private var checkedRadioButton: CompoundButton? = null
....
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    this.holder=holder

    val option = getItem(position)
    holder.txt_place_name.setText(option.toString())

    holder.itemView.radioButton.setOnCheckedChangeListener(checkedChangeListener)
    if (holder.itemView.radioButton.isChecked) checkedRadioButton = 
    holder.itemView.radioButton
    }
....
    private val checkedChangeListener = CompoundButton.OnCheckedChangeListener { 
    compoundButton, isChecked ->
    checkedRadioButton?.apply { setChecked(!isChecked) }
    checkedRadioButton = compoundButton.apply { setChecked(isChecked) }
    }
bz4sfanl

bz4sfanl6#

Kotlin2021

  • 在Adapter类的变量中声明RadioButton *
private var lastCheckedRB: RadioButton? = null
  • 现在,在适配器类onBindViewHolder中,将OnclickListener写入单选按钮 *
holder.YOURRADIOBUTTON.setOnClickListener {
        if (lastCheckedRB!=null){
            lastCheckedRB?.isChecked=false
        }
        lastCheckedRB = holder.YOURRADIOBUTTON
    }
5gfr0r5j

5gfr0r5j7#

尝试类似的方法:

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

上面的代码是在onCreateViewHolder()中注册到RadioGroup的监听器,前缀项是适配器中的字段。
onBindViewHolder()中:

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

应该是这样的

eivgtgni

eivgtgni8#

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

现在打印country_selcted[0],它仅为您提供行单选按钮组选择结果

f0brbegy

f0brbegy9#

要添加到G Anil Reddy answer:...

Kotlin2023

我变了

lastCheckedRB = holder.YOURRADIOBUTTON

致:

lastCheckedRB = if (lastCheckedRB == holder.YOURRADIOBUTTON) null else holder.YOURRADIOBUTTON

这是在用户多次点击同一个单选按钮的情况下。它将取消选中,并在每次用户点击时选中。
在Adapter类的变量中声明RadioButton

private var lastCheckedRB: RadioButton? = null

现在,在适配器类onBindViewHolder中,将OnclickListener写入RadioButton

holder.YOURRADIOBUTTON.setOnClickListener {
        if (lastCheckedRB!=null){
            lastCheckedRB?.isChecked=false
        }
        lastCheckedRB = if (lastCheckedRB == holder.YOURRADIOBUTTON) null else holder.YOURRADIOBUTTON
    }

相关问题