dimanche 21 janvier 2018

How do I keep checkbox states while scrolling in recyclerView?

I've looked here and here but I haven't been able to adapt these solutions to fix my problem. Can you help?

I check checkboxes, scroll down in my recyclerview, when I scroll back up, checkboxes are unchecked, or else different ones are checked.

I get why it's happening - as stated here: you should add a boolean variable into your onBindViewHolder to keep your item's selection status, which I believe I am doing with:

theContactsList.get(pos).setSelected(isChecked);

but still checkbox state is not preserved.

Here's my code:

  @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        //bind the views into the ViewHolder
        //selectPhoneContact is an instance of the SelectPhoneContact class.
        //We will assign each row of the recyclerview to contain details of selectPhoneContact:

        //The number of rows will match the number of phone contacts
        final SelectPhoneContact selectPhoneContact = theContactsList.get(position);

        //if the row is a matching contact
        if (viewHolder.getItemViewType() == 1)

        {
            //in the title textbox in the row, put the corresponding name etc...
            ((MatchingContact) viewHolder).title.setText(selectPhoneContact.getName());
            ((MatchingContact) viewHolder).phone.setText(selectPhoneContact.getPhone());
            //((MatchingContact) viewHolder).check.setText("Cheeckbox" + position);
            ((MatchingContact) viewHolder).check.setChecked(theContactsList.get(position).isSelected);
            ((MatchingContact) viewHolder).check.setTag(position);

          //  ((MatchingContact) viewHolder).check.setChecked(selectPhoneContact.isSelected());



            ((MatchingContact) viewHolder).check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           // ((MatchingContact) viewHolder).check.setOnClickListener(new CompoundButton.OnClickListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
                    //pos is the row number that the clicked checkbox exists in
                    Integer pos = (Integer) ((MatchingContact) viewHolder).check.getTag();

                    //set the value of the checkbox accordingly onCheckedChange,
                    //to true or false
                    theContactsList.get(pos).setSelected(isChecked);

                    if(isChecked == true ) {
                       // ((MatchingContact) viewHolder).check.setChecked(true);
                        Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " clicked!", Toast.LENGTH_SHORT).show();

                    }

                    else {
                       // ((MatchingContact) viewHolder).check.setChecked(false);
                        Toast.makeText(context_type, theContactsList.get(pos).getPhone() + " unclicked!", Toast.LENGTH_SHORT).show();

                    }

                }
            });

            }

    }




Aucun commentaire:

Enregistrer un commentaire