如何用RecyclerView android选择一个RadioButton?

时间:2017-09-21 12:32:31

标签: android android-recyclerview radio-button

我有一个带RadioButton的RecyclerView我只想在同一时间选择一个RadioButton并且我的代码使它工作正常,但当我从上到下重复选择时,选择消失我怎么能修复它请谢谢

enter image description here

TextView

4 个答案:

答案 0 :(得分:5)

你可以这样做,使用RadioButton我只是写而不检查但这是一个提示 -

 private CompoundButton lastCheckedRB = null;

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
    ContactUsModel contactUsModel = orderList.get(position);
    holder.orderNum.setText(contactUsModel.getOrderNum());
    holder.orderCost.setText(contactUsModel.getOrderCost());
    holder.readioBtn.setOnCheckedChangeListener(ls);
    holder.readioBtn.setTag(position);
}

private OnCheckedChangeListener ls = (new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {
        int tag = (int) buttonView.getTag();
        ;
        if (lastCheckedRB == null) {
            lastCheckedRB = buttonView;
        } else if (tag != (int) lastCheckedRB.getTag()) {
            lastCheckedRB.setChecked(false);
            lastCheckedRB = buttonView;
        }

    }
});

答案 1 :(得分:1)

orderList

中使用一个临时变量

ex:isSelected = false;

确保将其添加到arrayList

的所有对象中

当您选择任何单选按钮时,只需更新当前位置orderList isSelected = true并保留isSelected = false;

并致电notifyDataSetChanged()

还要确保您正在检查

if(isSelcted)
redioButton.setChecked(true);
else
redioButton.setChecked(false);

例如:  if(csSelectedAddressPos == position)                 holder.radioBtn.setChecked(真);             其他                 holder.radioBtn.setChecked(假);

        holder.radioBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //set object isChecked here 
                csSelectedAddressPos = position;
                notifyDataSetChanged();
            }
        });

答案 2 :(得分:0)

您无需保持职位Tag或重新加载列表notifyDataSetChanged()。 只需使用checkedRadioButton并将其切换即可。然后,您可以使用checkedRadioButton来获取所选项目。

private var checkedRadioButton: CompoundButton? = null

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    holder.itemView.radio_button.setOnCheckedChangeListener(checkedChangeListener)
    if (holder.itemView.radio_button.isChecked) checkedRadioButton = holder.itemView.radio_button
}

private val checkedChangeListener = CompoundButton.OnCheckedChangeListener { compoundButton, isChecked ->
    checkedRadioButton?.apply { setChecked(!isChecked) }
    checkedRadioButton = compoundButton.apply { setChecked(isChecked) }
}

答案 3 :(得分:-3)

1st:获取个人单选按钮ID。 第二步:将点击监听器分配给个人单选按钮。

说你有6个单选按钮:

import time
from holoviews.streams import Stream

dmap = hv.DynamicMap(BokehDisplay, streams=[Stream.define('next')()])
dmap  # Display the DynamicMap here

while True:  # In a new notebook cell
   time.sleep(10)
   dmap.event()

//同样为所有单选按钮添加侦听器:

{



radioButton1.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        radioButton1.setChecked(true);
                        radioButton2.setChecked(false);
                        radioButton3.setChecked(false);
                        radioButton4.setChecked(false);
                        radioButton5.setChecked(false);
                        radioButton6.setChecked(false);
                    }
                });