svvorf svvorf - 5 months ago 30
Android Question

RecyclerView keeps repeating cached (?) subview and not looking to onBindViewHolder

I have RecyclerView where every list item has an ImageButton, thee image of which I define in the adapter's onBindViewHolder():

int myVote = getMyVote();
if (myVote != 0) {
Log.d("dbg", myVote + "");
holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
}


enter image description here

So ratingButton is a star in the right bottom corner of the list item layout. Its shape is filled with gray color (and accordingly, a log record is pushed) if the condition (myVote != 0) is satisfied.

The problem is that when I scroll the list down I can watch other stars became filled even though I can see the only one record in the log window (for the correct list item). Moreover, this list items with incorrectly changed buttons repeat every 5 rows, and that's what's confusing me. If I change
mListView.setItemViewCacheSize(0);
the repeat period changes to 3, so we can assume it somehow connected with the RecyclerView's caching and recycling mechanism.

Please, help me to work the problem out. Thanks!

Answer

Try to change your code to:

if (myVote != 0) {
        Log.d("dbg", myVote + "");
        holder.ratingButton.setImageResource(R.drawable.ic_star_grey600_36dp);
    } else {
        holder.ratingButton.setImageResource(int another resource);
    }
}
Comments