Solution for shuffling checked positions while scrolling RecyclerView with CheckBox

All u need to do is follow these in model class
1. implement ur model class with Serialize interface
2. include a boolean variable

import java.io.Serializable;

public class Student implements Serializable {

 /**
  * 
  */
 private static final long serialVersionUID = 1L;

 private String name;

 private String emailId;

 private boolean isSelected;

 public Student() {

 }

 public Student(String name, String emailId) {

  this.name = name;
  this.emailId = emailId;

 }

 public Student(String name, String emailId, boolean isSelected) {

  this.name = name;
  this.emailId = emailId;
  this.isSelected = isSelected;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getEmailId() {
  return emailId;
 }

 public void setEmailId(String emailId) {
  this.emailId = emailId;
 }

 public boolean isSelected() {
  return isSelected;
 }

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

}

And the adapter should be the same

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

 private List<Student> stList;

 public CardViewDataAdapter(List<Student> students) {
  this.stList = students;

 }

 // Create new views
 @Override
 public CardViewDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
   int viewType) {
  // create a new view
  View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
    R.layout.cardview_row, null);

  // create ViewHolder

  ViewHolder viewHolder = new ViewHolder(itemLayoutView);

  return viewHolder;
 }

 @Override
 public void onBindViewHolder(ViewHolder viewHolder, int position) {

  final int pos = position;

  viewHolder.tvName.setText(stList.get(position).getName());

  viewHolder.tvEmailId.setText(stList.get(position).getEmailId());

  viewHolder.chkSelected.setChecked(stList.get(position).isSelected());

  viewHolder.chkSelected.setTag(stList.get(position));

  
  viewHolder.chkSelected.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    CheckBox cb = (CheckBox) v;
    Student contact = (Student) cb.getTag();

    contact.setSelected(cb.isChecked());
    stList.get(pos).setSelected(cb.isChecked());

    Toast.makeText(
      v.getContext(),
      "Clicked on Checkbox: " + cb.getText() + " is "
        + cb.isChecked(), Toast.LENGTH_LONG).show();
   }
  });

 }

 // Return the size arraylist
 @Override
 public int getItemCount() {
  return stList.size();
 }

 public static class ViewHolder extends RecyclerView.ViewHolder {

  public TextView tvName;
  public TextView tvEmailId;

  public CheckBox chkSelected;

  public Student singlestudent;

  public ViewHolder(View itemLayoutView) {
   super(itemLayoutView);

   tvName = (TextView) itemLayoutView.findViewById(R.id.tvName);

   tvEmailId = (TextView) itemLayoutView.findViewById(R.id.tvEmailId);
   chkSelected = (CheckBox) itemLayoutView
     .findViewById(R.id.chkSelected);

  }

 }

 // method to access in activity after updating selection
 public List<Student> getStudentist() {
  return stList;
 }

}

Comments

  1. Really it is very useful for us..... the information that you have shared is really useful for everyone. If someone wants to know about EHS Software and Health and Safety Software I think this is the right place for you.
    Safety Software | Employee Management Software

    ReplyDelete

Post a Comment

Popular Posts