-1

I am implemeting multiple selection on GridManager using RecyclerView.

Here is my code inside adapter

imgStamps.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    //First setting up isSelected() or not

                    if (imageList.get(getAdapterPosition()).isSelected()) {
                        imageList.get(getAdapterPosition()).setSelected(false);
                    } else {
                        imageList.get(getAdapterPosition()).setSelected(true);
                    }

                   //Setting blur image on Imageview onLongclick and resting on again press.
                    if (imageList.get(getAdapterPosition()).isSelected()) {
                        mCount++;
                        imgBlurr.setVisibility(View.VISIBLE);
                    } else {
                        mCount--;
                        imgBlurr.setVisibility(View.GONE);
                    }
                    mCommunicator.clicked(mCount, getAdapterPosition());
                    return true;
                }
            });

The above code is inside ViewHolder not onBindViewHolder.

If I am selectimg first image and scrolls down and then up the view gets reset.

Can the mistake or behaviour can be pointed out?

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
  • Are you setting the visibility appropriately in `onBindViewHolder()` too? If not, that's your problem. – Mike M. Dec 21 '18 at 06:49

2 Answers2

1

RecyclerView reuses your layout. Put your logic on onBindViewHolder method.

For more information explore this question How to properly highlight selected item on RecyclerView?

Ehsan Mashhadi
  • 2,568
  • 1
  • 19
  • 30
0

RecyclerView will reused your item view when you scroll. To manager multi select you must have an array of selected position (or selected model). And onBindViewHolder, check position in this array to check item selected or not. For more details implement, please refer to : Multi selection in RecyclerView?

Community
  • 1
  • 1
Cuong Nguyen
  • 222
  • 3
  • 8