如何只选择一个带有RecyclerView android的RadioButton? [英] how to select just one RadioButton with RecyclerView android?

查看:586
本文介绍了如何只选择一个带有RecyclerView android的RadioButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有RadioButton的RecyclerView,我只想同时选择一个RadioButton,而我的代码使它正常工作,但是当我从上到下重复选择时,选择消失了,我该如何解决它,谢谢

I have a RecyclerView with RadioButton I just want to select one RadioButton not many in the same time and my code make it works fine, but when I repeat the selection from top to bottom the select disappear how can I fix it please thanks.

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;


        }
    });

推荐答案

您可以这样做,使用RadioButton,我只是在不加检查的情况下编写,但这是对您的提示-

You can do as,Use RadioButton I just write without check But this is an hint for you -

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

    }
});

这篇关于如何只选择一个带有RecyclerView android的RadioButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆