Skip to content Skip to sidebar Skip to footer

Recycled Recyclerview Item Keeps The Old Background Color

I have a recycler view and inside the onClick(View view) i'm changing the background color to almost transparent red view.setBackgroundColor(Color.argb(64, 183, 28, 28)); but somet

Solution 1:

The simplest way to achieve this in my opinion is to give each item in your RecyclerView a state (boolean) and set the background depending on the state. This way when you recycle the view, the correct background is drawn.

if(isClicked){
    itemView.setBackgroundColor(Color.RED);
}
else{
    itemView.setBackgroudnColor(Color.WHITE);
}

Solution 2:

You should set all attributes in your onBindViewHolder(), because, as you can see, view may not be fresh but recycled and show state of previously presented record. So if background color maters, you must set it too in onBindViewHolder().

Solution 3:

You need to maintain the state of the background colour outside the ViewHolder (maybe in the Adapter or higher up). Then, in the onBindViewHolder of your Adapter, you set the background colour of your ViewHolder based on that state.

Post a Comment for "Recycled Recyclerview Item Keeps The Old Background Color"