0

I have a recyclerView and have implemented onLongClick listener on the items. The Adapter is in the same class as the Activity.

I created a set: public Set<Integer> multiplePositions as instance variable of the Activity class and am initialising it in onCreate() method as multiplePositions = new TreeSet<>().Coming to my Adapter class in the onBindViewHolder method I created a click listener as follows:

holder.textCardView.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View v) {

                    Adapter.this.onLongClick(holder, position);

                    return true;
                }
            });
            holder.textCardView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                }
            });

As you can see I am calling the method onLongClick. My onLongClick method looks like this:

public void onLongClick(ViewHolder holder, int position) {

        multiplePositions.add(position);

        for(Integer i : multiplePositions) {
            Log.e("set", ""+i);


        }
        holder.textCardView.setBackgroundResource(R.color.long_press);

}

Here, whenever an item clicked I am adding the position in the Set but I don't know how do I iterate through this set and set the background color of all the item at the positions in the set.

I am aware that this can be achieved by creating a boolean variable isSelected in the model class but if I go with this method then other functionalities of my code won't work.

My main concern is if I scroll the recyclerView up or down then color of the selected positions should not disappear. The unselection part will be done in setOnClickListener().

Shaifali Rajput
  • 1,279
  • 12
  • 30
localhost
  • 107
  • 1
  • 11
  • see this http://droidmentor.com/multi-select-like-whatsapp-android/ – Vij Sep 21 '17 at 12:29
  • do not add any listeners inside `onBindViewHolder` - why dont you simply follow the code i gave you? you want the solution without `boolean isSelected`? then use `SparseBooleanArray` - set it in click listener an read it inside `onBindViewHolder` – pskink Sep 21 '17 at 13:16
  • so this [Adapter](https://pastebin.com/raw/ttpL00cM) uses `SparseBooleanArray`, does it differ much from "booolean flag" in data model? no, not really – pskink Sep 21 '17 at 13:38
  • OK you have to decide if you want it to be working or not – pskink Sep 22 '17 at 06:01
  • I want it working and right now I am modifying the code with the one which you gave me – localhost Sep 22 '17 at 06:07

1 Answers1

0

Use SparseBoolean array to store the positions of the items clicked in the reyclerview.Check the answer in this link

https://stackoverflow.com/questions/46336645/hilighted-selected-items-colour-changes-when-not-in-view-in-recyclerview/46336844#46336844

Check here the purpose of using SparseBoolean array

https://stackoverflow.com/questions/18822708/what-is-the-clear-purpose-of-sparsebooleanarray-i-referred-official-android

EDIT: Use SparseBooleanArray to keep track of selected items in recycler view adapter

Initialize the SparseBooleanArray as private memeber variable

private SparseBooleanArray mClickedItems=new SparseBooleanArray();

Inside your click function while clicking textCardView make the item position as true.

            holder.textCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickedItems.put(position,true);
               notifyDataSetChanged();

            }
            });

Then in the onBindViewHolder check if the position is already selected or not like this

if(mClickedItems.get(position)==true){
        //Show selected color
    holder.textCardView.setBackgroundResource(ContextCompat.getColor(holder.textCardView.getContext(),R.drawable.selected));                                              

    }else {
        //show unselected color

   holder.textCardView.setBackgroundResource(ContextCompat.getColor(holder.textCardView.getContext(),R.drawable.unselected));
    }
Sharath kumar
  • 4,064
  • 1
  • 14
  • 20