vendredi 29 décembre 2017

On Scrolling Check Boxes are getting Unchecked or Vice versa in android RecyclerView

I have a check box in RecyclerView Adapter's View Item. When scrolling, some check boxes are getting checked and some are getting unchecked. I have taken help from this and this But I am unable to find the exact solution to this.

Here is my code

 @Override
public void onBindViewHolder(final AllContactsAdapter.ViewHolder holder, final int position) {

    phoneContactsModel = listOfContacts.get(position);
    holder.name.setText(phoneContactsModel.getContactName());
    holder.phoneNumber.setText(phoneContactsModel.getContactNumber());
    holder.checkBox.setOnCheckedChangeListener(null);
    holder.checkBox.setSelected(phoneContactsModel.isChecked());

    holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            phoneContactsModel.setChecked(b);

        }
    });

    holder.checkBox.setSelected(phoneContactsModel.isChecked());

    holder.contactLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (phoneContactsModel.isChecked()) {
                holder.checkBox.setSelected(false);
                copyOfListOfContacts.remove(listOfContacts.get(position));

            } else {
                holder.checkBox.setSelected(true);
                copyOfListOfContacts.add(listOfContacts.get(position));

            }
        }
    });
}

For example, I have checked 1,2,3 items in list. When I scrolled down and come up , then 13 is checked and 2 is unchecked along with other some items.Please help me.

Any solution is appreciated.

Thank You.




Aucun commentaire:

Enregistrer un commentaire