如何使用复选框处理Recyclerview状态?

时间:2016-07-22 17:26:00

标签: android checkbox android-recyclerview

我正在使用recyclerview。我的recyclerview从列表中填充,并且在填充后它具有两种状态的复选框。但是当我在特定位置检查或取消选中时以及在其前一个位置上滚动它之后我有一些问题。 我的代码如下所示。

@Override

public void onBindViewHolder(final AttendanceHolder holder, final int position) {


    holder.statusCheckBox.setOnCheckedChangeListener(null);
    if(childrenListForAttendance.get(position).getStatus()==0) {
        holder.statusCheckBox.setTag(childrenListForAttendance.get(position));
        holder.childName.setText(childrenListForAttendance.get(position).getMchildName());
        holder.statusCheckBox.setChecked(childrenListForAttendance.get(position).isSelected());
    }
    else{
        holder.statusCheckBox.setTag(childrenListForAttendance.get(position));
        holder.childName.setText(childrenListForAttendance.get(position).getMchildName());
        holder.statusCheckBox.setChecked(false);
    }

    holder.statusCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            childrenListForAttendance.get(position).setIsSelected(isChecked);
        }
    });

}

    @Override
    public int getItemCount()

    {
        return childrenListForAttendance.size();
    }

如何处理这种情况。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

我没有看到正确的设置检查:

holder.statusCheckBox.setChecked(true);

所以你正确地取消选中,但是什么时候是真的你永远不会选中复选框。

您的代码修改:

holder.statusCheckBox.setOnCheckedChangeListener(null);

//NOT_USED constant with value of status not used
if(childrenListForAttendance.get(position).getStatus()==NOT_USED){

  //code for not used checkbox
}
//is checked is constant with value of checked status
else if(childrenListForAttendance.get(position).getStatus()==IS_CHECKED) {

     holder.statusCheckBox.setChecked(true);
     //other code
}
//code for uncheck
else{

    holder.statusCheckBox.setChecked(false);
    //other code
}

holder.statusCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        childrenListForAttendance.get(position).setIsSelected(isChecked);
    }
});

答案 1 :(得分:1)

Recyclerview中的CheckBox问题是,由于回收物视图的回收而导致视图被回收,并且未维护Checkbox(检查或取消选中)的值

你可以看看我的例子。你可以这样做:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private ArrayList<Model> myItems = new ArrayList<>();

    public MyAdapter (ArrayList<Model> getItems, Context context){
        try {
            mContext = context;
            myItems = getItems;
            }catch (Exception e){
            Log.e(FILE_NAME, "51: " + e.toString());
            e.printStackTrace();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tView;
        public CheckBox cBox;

        public ViewHolder(View v) {
            super(v);
            tView = (TextView) v.findViewById(R.id.textView);
            cBox = (CheckBox) v.findViewById(R.id.checkBox);
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        final ObjectIncome objIncome = myItems.get(position);
        String content = "sample String";
        holder.tView.setText(content);

        //in some cases, it will prevent unwanted situations
        holder.cBox.setOnCheckedChangeListener(null);

        //if true, your checkbox will be selected, else unselected
        if(holder.statusCheckBox.get(position).getStatus()==IS_CHECKED) {

            holder.statusCheckBox.setChecked(true);

      }
      else{

            holder.statusCheckBox.setChecked(false);

       }
        holder.cBox.setChecked(Model.get(position).isSelected());

        holder.cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    //set your object's last status
                    Model.get(position).setSelected(isChecked);
            }
        });

    }
}