user13608097
user13608097

Reputation: 86

Android - Color repeat on scrolling in RecyclerView

I have a RecyclerView in which a selected number of items show a different background color.

It gave me with many views with different color.

I used the position of adapter to change color and later I found that position changes when scrolling.

So I gave a unique code to every object of the ArrayList. And I gave the program to change background color if the unique code matches the given code.

Like,

    uniqueCode = 5;

public void onBindViewHolder(//..){

    Object object = objectArrayList.get(i);

    if(object.uniqueCode() == uniqueCode  ){
        holder.layout.setBackgroundColor(//....);
    }
}

But still I get some views changes the background color on scrolling which does not match that unique Id.

What is the solution to this problem ?

Upvotes: 1

Views: 886

Answers (1)

K.Kotsi
K.Kotsi

Reputation: 86

If i understand correctly there are multiple cells with the "unique" color but it shouldn't. This happens because you never reset the background of the layout to the default color.

if(object.uniqueCode() == uniqueCode){
    holder.layout.setBackgroundColor(/*unique color*/);
}
else{
    holder.layout.setBackgroundColor(/*default color*/);
}

Upvotes: 2

Related Questions