Android 获取回收器视图中所有项目中单选按钮的选中状态

Android 获取回收器视图中所有项目中单选按钮的选中状态,android,android-recyclerview,Android,Android Recyclerview,我有一个RecyclerView,其中包含10个视图,每个视图都有一个复选框。现在,在我的主要活动中,当按下名为“POST”的菜单按钮时,我想知道是否选中了回收视图每个视图中的所有复选框 如何实现这一点?我建议您在传递给RecyclerView的模型列表的每个模型中传递附加变量isChecked。 就像这样: public class Model { private boolean isChecked; public boolean isChecked() {

我有一个
RecyclerView
,其中包含10个视图,每个视图都有一个
复选框。现在,在我的主要活动中,当按下名为“POST”的菜单按钮时,我想知道是否选中了
回收视图
每个视图中的所有
复选框


如何实现这一点?

我建议您在传递给RecyclerView的模型列表的每个模型中传递附加变量
isChecked
。 就像这样:

public class Model {
    private boolean isChecked;

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }
}
然后在您的RecyclerView ViewHolder中,创建一个构造函数:

public ListViewHolder(View view) {
   super(view);
   switchCompat = (SwitchCompat) view.findViewById(R.id.add_switch);
   switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                getItemAt(getLayoutPosition()).setChecked(isChecked);
            }
   });
}
然后,要获取所有按钮状态,只需遍历活动中的模型列表:

public boolean areAllChecked() {
    for (int i = 0; i < adapter.getItemCount(); i++) {
         Model model = adapter.getItemAt(i);
         if (!model .isChecked()) {
             return false;
         }
    }
    return true;
}
public boolean areAllChecked(){
对于(int i=0;i
  • 在模型类中添加布尔值
    isChecked
    (要添加到
    RecyclerView
    适配器的项)
  • 在适配器
    onBindViewHolder
    中,为复选框实现
    onCheckedChangeListener
    ,并适当设置
    isChecked
  • 在适配器中实现
    isChecked(int位置)
    方法。它应该返回
    position
  • 在片段/活动中,遍历适配器项,并确定是否已选中所有项

  • 跟踪适配器中每个复选框的状态。 为此,让它有一个映射来存储绑定到每个复选框的唯一键的布尔值。并让它实现OnCheckedChangeListener

    MyAdapter扩展了RecyclerView。适配器实现OnCheckedChangeListener{
    私有映射checkboxStates=new HasMap();
    @凌驾
    检查更改后的公共无效(复合按钮视图,布尔值已检查){
    checkboxStates.put(buttonView.getTag(),已选中);
    }
    公共映射getCheckBoxState(){
    //我们不希望调用方修改适配器状态。
    返回Collections.unmodifiableMap(this.checkboxStates);
    }
    //其他代码
    @凌驾
    公共无效onBindViewHolder(ViewHolder,int位置){
    //你的代码
    复选框cb;
    //假设我们有对cb视图的引用
    //还假设cb具有从模型分配的唯一可识别标记
    cb.setOnCheckedChangeListener(此);
    if(this.checkboxStates.containsKey(cb.getTag()){
    setChecked(this.checkboxStates.get(cb.getTag());
    }否则{
    this.checkboxStates.put(cb.getTag(),cb.isChecked());
    }
    }
    }
    
    这样,您就可以调用getCheckBoxState来获取每个可见复选框的状态

    这里的关键点是,对于数据集中的每个项目,您需要一些唯一的可识别项,这些项可以用作表示该项目的每个复选框的标记

    MyAdapter extends RecyclerView.Adapter implements OnCheckedChangeListener {
       private Map<Object, Boolean> checkboxStates = new HasMap<>();
    
       @Override
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          checkboxStates.put(buttonView.getTag(), isChecked);
       }
    
       public Map<Object, Boolean> getCheckboxStates() {
         //We don't want the caller to modify adapter state.
         return Collections.unmodifiableMap(this.checkboxStates);
       }
    
       //other code
       @Override
       public void onBindViewHolder(ViewHolder holder, int position) {
         //your code
         CheckBox cb; 
         //assuming that we have the reference to the cb view
         //also assuming that cb has a unique identifiable tag assigned from the model
         cb.setOnCheckedChangeListener(this);
    
         if (this.checkboxStates.containsKey(cb.getTag()) {
           cb.setChecked(this.checkboxStates.get(cb.getTag());
         } else {
           this.checkboxStates.put(cb.getTag(), cb.isChecked());
         }
       }
    }