Reputation: 219
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
Upvotes: -2
Views: 407
Reputation: 219
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....
Upvotes: 0
Reputation: 24211
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.
Upvotes: 0