-2

I am working with recyclerview and i know how to change the color of selected item...

I am using SparseBooleanArray to change change the color of multiple items but don't know how to change the color of all items when user touch on selectAll button

remaining things working fine like get all the items in arraylist but don't know how to change the color of background at the same time....

Please anyone can suggest me...and comment for code if you want if unable to comment put it in answer which class you need for giving me suggestion

Vipul Chauhan
  • 189
  • 4
  • 19
  • Take a look at this SO post on [Choice Mode in a RecyclerView](https://stackoverflow.com/questions/28651761/choice-mode-in-a-recyclerview) – Bö macht Blau Mar 14 '19 at 19:05
  • anyhow can i achieve it without using another library – Vipul Chauhan Mar 14 '19 at 19:07
  • No libraries: [www.bignerdranch.com RecyclerView Part 2: Choice Modes](https://www.bignerdranch.com/blog/recyclerview-part-2-choice-modes/) – Bö macht Blau Mar 14 '19 at 19:10
  • Possible duplicate of [How to implement multi-select in RecyclerView?](https://stackoverflow.com/questions/36369913/how-to-implement-multi-select-in-recyclerview) – Bö macht Blau Mar 14 '19 at 19:12
  • I knew there had to be a good SO post somewhere :D – Bö macht Blau Mar 14 '19 at 19:13
  • Their Model class combines the data with the info about selected state. Other approaches use the original data list combined with a new List (or array) for keeping track of selected rows. I like this answer very much because it gives you the complete code plus explanations of single steps - if you have trouble understanding anything you can always set up a small sample app and turn on the debugger – Bö macht Blau Mar 14 '19 at 19:34
  • please see the updated question and suggest me how to implement with this code – Vipul Chauhan Mar 15 '19 at 18:01
  • please see the updated question – Vipul Chauhan Mar 16 '19 at 15:01

2 Answers2

0

You just have to keep the track of the colors of each item in a separate array and then in your onBindViewHolder while populating each item in your RecyclerView, get the status of the background colors from that array.

You have a click listener for each item I assume. When you are about to change the background color, just keep an array of the items and update the value of the item accordingly when clicked. For example, you might consider having the following array.

int[] selectedItems = new int[yourArrayList.size()]; // Initially all items are initialized with 0

Then in your onBindViewHolder you need to do assign 1 when an item is selected.

public void onClick(int position) {
    // Change the background here

    // Keep track of the items selected in the array
    if (selectedItems[position] == 0) 
        selectedItems[position] = 1; // The item is selected
    else selectedItems[position] = 0; // The item is unselcted
}

if(selectedItems[position] == 1) itemView.setBackgroundColor(andriod.R.color.gray);
else itemView.setBackgroundColor(andriod.R.color.white);

Hope that helps you to understand.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
0

I found an answer for my question and i think it is the easiest way to do this:

Create an arraylist of view type...

Arraylist<View> view=new ArrayList();

class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener, View.OnClickListener {
private ImageView thumbnail_img;private TextView SongName;LinearLayout layout;
private ItemViewHolder(View itemView) {
    super(itemView);
    itemView.setOnLongClickListener(this);
    itemView.setOnClickListener(this);
    view.add(itemView);

    thumbnail_img=itemView.findViewById(R.id.album_art);
    SongName=itemView.findViewById(R.id.song_test);

   }
}

After that when you want to use it

for(View v:view){
     v.setBackground(context.getResources().
            getDrawable(android.R.color.transparent));
    }

You can use it also by position....

View v= view.get(index); //index is the int value for which you want to get the view.

Try and enjoy this simple code....

Vipul Chauhan
  • 189
  • 4
  • 19