【问题标题】:android - Single RadioButton in RecyclerViewandroid - RecyclerView 中的单个 RadioButton
【发布时间】:2017-07-11 11:27:11
【问题描述】:

我有一个 RecyclerView,其中的项目仅包含 RadioButton,我的适配器创建了具有某些位置的 RecyclerView - 可以是 5-10 个位置,每个位置都有 RadioButton。但是这些 RadioButton 不在同一个 RadioGroup 中,因为它们都在不同的 RecyclerView 位置。有没有办法为它设置单一选择?

PS:我找到了这个Select only one radiobutton in a recyclerview,但这都是关于 RecyclerView 位置的 RadioGroups,我只有 RadioButtons。

【问题讨论】:

    标签: java android android-recyclerview android-radiobutton


    【解决方案1】:

    您可以通过使用boolean 变量isChecked 创建模型类来管理它。

    当您选择RadioButton 时,首先为所有操作执行isChecked = false,然后为您选择的按钮设置为true,然后调用notifyDataSetChanged

    在适配器中使用

    radioButton.setChecked(list.get(position).isChecked())
    

    它肯定会帮助你。

    【讨论】:

    • 如果列表中有超过 100 或 1000 个项目怎么办。
    • 是的,我知道了,但是我的 RecyclerView 是从 ArrayList 构造的,其中包含定义的 ObjectModels。
    • @SanjayMajoka 如果它有超过 100 个项目不会有任何问题。这是您可以做的最简单的方法。还有另一种方法是保留以前的视图并在之前取消选中它并检查新视图
    • @Den 然后在ObjectModels 中取一个新变量并用它来管理它。
    • @Den 使用此参考,它将为您提供更多帮助 stackoverflow.com/a/36930230/6676466 在此,它通过持有 radioButton 的先前视图来管理它。
    【解决方案2】:

    我遇到了同样的问题。按照 Vishal Chhodwani 的回答,我编写了以下解决方案,希望它可以帮助其他读者。

    class MyRecycler extends RecyclerView.Adapter<MyRecycler.RecyclerViewHolder> {
    
        static class RecyclerViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.aRadioButton)
            RadioButton rb;
    
            public RecyclerViewHolder(View itemView, int viewType, final Context context) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            } 
        }
    
        private List<MyModel> list;
        private RadioButton selectedRadioButton;  
    
        // Constructor and other methods here...
    
        @Override
        public void onBindViewHolder(final MyRecycler.RecyclerViewHolder holder, int position) {
    
            RadioButton radioButton = holder.rb;
            radioButton.setChecked(list.get(position).isChecked());
    
            radioButton.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    // Set unchecked all other elements in the list, so to display only one selected radio button at a time 
                    for(MyModel model: list)
                        model.setChecked(false);
    
                    // Set "checked" the model associated to the clicked radio button
                    list.get(position).setChecked(true);
    
                    // If current view (RadioButton) differs from previous selected radio button, then uncheck selectedRadioButton
                    if(null != selectedRadioButton && !v.equals(selectedRadioButton))
                        selectedRadioButton.setChecked(false);
    
                    // Replace the previous selected radio button with the current (clicked) one, and "check" it
                    selectedRadioButton = (RadioButton) v;
                    selectedRadioButton.setChecked(true);                    
    
            });
        }
    }
    

    【讨论】:

      【解决方案3】:

      KOTLIN 2021 测试

      与数据列表大小无关

      单选 RecyclerView,只有 RadioButton,没有 RadioGroup 并且经过测试

      在 Adapter 类中的变量中声明 RadioButton:

      private var lastCheckedRB: RadioButton? = null
      

      现在在您的 Adapter 类 onBindViewHolder 中,将 OnclickListener 写入 RadioButton:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 1970-01-01
        • 1970-01-01
        • 2014-09-24
        • 2012-01-06
        相关资源
        最近更新 更多