ANITHA SUNDARAMURTHY ANITHA SUNDARAMURTHY - 3 years ago 95
Android Question

Recycler View Wrongly selecting View Item while performing multi-Select

I have implemented an recycler view with Multi Select successfully using addOnItemTouchListener for identifying and selecting using single and double click.When i have Less than 5 items int the recycler view,Everything works fine but when i have more data,to be exact when the recycler view starts recycling the view,my selection is going crazy and it selects differnt possition.I logged out to see the clicking position,those seems to be right but something is not right which makes my selection wrong.I'm new to programming and to android app development.Can anyone take alot at this and help me out please,Here is my code

Answer Source

You are doing most of the view manipulation in Onclick listener, you should move that to you adapter onBindView, because even if you set in Onclick listener, it will modify when user scroll.

I am not giving complete view manipulation, I am just giving the hint how we should do.

@Override
public void onBindViewHolder(final ContactHolder holder, int position) {
    pos = position;
    final Contact contact = contactList.get(pos);

    holder.colg.setText(contact.getColg());
    holder.name.setText(contact.getName());
    holder.job.setText(contact.getJob());


    if (contact.getImage() != null)
        holder.img.setImageBitmap(Utility.getPhoto(contact.getImage()));


    if (multiselect_list.contains(contactArrayList.get(position))) { // May be you should check form your fragment or actvity using listeners
        holder.mentee.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));
        holder.participant.setBackgroundColor(ContextCompat.getColor(ContactsActivity.this, R.color.colorPrimary));

        holder.mentee.setVisibility(View.VISIBLE);
        holder.participant.setVisibility(View.VISIBLE);
    } else {

        holder.mentee.setVisibility(View.GONE);
        holder.participant.setVisibility(View.GONE);
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download