如何用RecyclerView android选择一个单选按钮?

问题描述 投票:1回答:4

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

enter image description here

private RadioButton 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.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            RadioButton checked_rb = (RadioButton) group.findViewById(checkedId);

            if (lastCheckedRB != null &&lastCheckedRB.isChecked()) {
                lastCheckedRB.setChecked(false);
            }
            //store the clicked radiobutton
            lastCheckedRB = checked_rb;


        }
    });
android android-recyclerview radio-button
4个回答
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
投票

orderList中使用一个临时变量

例如:isSelected = false;

确保将其添加到arrayList的所有对象中

当你选择任何单选按钮时,只需更新当前位置orderList isSelected = true和剩余的isSelected = false;

并致电notifyDataSetChanged()

还要确保你正在检查

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

例如:if(csSelectedAddressPos == position)holder.radioBtn.setChecked(true); else holder.radioBtn.setChecked(false);

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

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
投票

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

说你有6个单选按钮:

{



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

//类似地将监听器添加到所有单选按钮:

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

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


}
© www.soinside.com 2019 - 2024. All rights reserved.