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.