RecyclerView中的多选

时间:2015-11-16 11:39:21

标签: android android-recyclerview

我有CheckBox的数据列表。我需要从RecyclerView检查或取消选中我的复选框。当我尝试这个时,选中了多个复选框。

public class AttendanceAdapter extends RecyclerView.Adapter<AttendanceAdapter.MyStudentsViewHolder>{
    private LayoutInflater inflater;
    private Context contexts;
    List<studinformation> data= Collections.emptyList();
    public AttendanceAdapter(Context context,List<studinformation> data){
        inflater=LayoutInflater.from(context);
        this.data=data;
        this.contexts=context;
    }

    @Override
    public MyStudentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= inflater.inflate(R.layout.customrow_students,parent,false);
        MyStudentsViewHolder holder=new MyStudentsViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyStudentsViewHolder holder, int position) {
        studinformation current=data.get(position);
        holder.studentid.setText(current.studID);
        holder.studentname.setText(current.studName);
        holder.studentid.setSelected(true);
    }

    @Override
    public int getItemCount() {

        return data.size();
    }
    class MyStudentsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CheckBox studentid;
        TextView studentname;
        public MyStudentsViewHolder(View itemView) {
            super(itemView);
            studentid= (CheckBox) itemView.findViewById(R.id.ChkSid);
            studentname= (TextView) itemView.findViewById(R.id.textName);
            studentid.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            Toast.makeText(contexts, "Item Clicked At" + getPosition(), Toast.LENGTH_SHORT).show();
            if(getPosition()==0) {


               // Intent intent = new Intent(contexts, SubActivity.class);
               // contexts.startActivity(intent);
            }

        }


    }


}

2 个答案:

答案 0 :(得分:1)

RecyclerView将重复使用view.so滚动后返回它会重置正在发生的数据,因为您没有选中是否选中了复选框。

StudInformation模型类中创建属性isSelected

public class StudInformation
{
  private boolean isSelected=false;

  public void setSelected(boolean param)
  {
    this.isSelected=param;
  }
  public boolean isSelected()
  {
   return this.isSelected;
   }
}

内部onClick

  @Override
    public void onClick(View v) {
     data.get(getLayoutPosition()).setSelected(studentid.isChecked());
    }

onBindViewHolder

@Override
public void onBindViewHolder(MyStudentsViewHolder holder, int position) {
    .....
    studinformation current=data.get(position);
    holder.studentid.setSelected(current.isSelected());

}

Cross reference link

答案 1 :(得分:0)

您必须使用setTag()方法获取每个复选框的位置。 使用此代码代替holder.studentid.setSelected(true);并删除studentid.setOnClickListener(this);

holder.studentid.setChecked(data.get(position).isSelected());

holder.studentid.setTag(data.get(position));


holder.studentid.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
String id = String.valueOf(cb.getTag());
Toast.makeText(
  v.getContext(),
  "Clicked on Checkbox: " + id, Toast.LENGTH_LONG).show();
  }
});