2015-11-16 74 views
1

我有CheckBox的數據列表。我需要從我的RecyclerView中選中或取消選中我的複選框。當我嘗試選擇多個複選框時。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); 
      } 

     } 


    } 


} 
+0

考慮接受或評論下面的答案。 – Stella

回答

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