Reputation: 229
So essentially, I have a recyclerview that has a list of levels. But essentially, I want certain items to have a black colored background to suggest that they are not locked yet.(kind of like in video games)
my current method:
@Override
public void onBindViewHolder(BracketsAdapter.ViewHolder viewHolder, int position) {
// Get the data model based on position
Bracket bracket = bracketsList.get(position);
int color = UserPreferences.getInstance().getBracket();
// Set item views based on your views and data model
TextView textView = viewHolder.nameTextView;
textView.setText(bracket.toString());
Log.d("Bracket level", ""+bracket.getId());
if(color < bracket.getId()) {
textView.setBackgroundResource(R.color.black); // suggests level is locked
}
}
The issue with this method, is that it works initially but the moment I scroll down and back up, items that should not be colored end up being colored. Why is this? Is there a better way of implementing this?
Upvotes: 1
Views: 247
Reputation: 11642
Recycle view always reuses the old view so you are getting that problem, To solve the problem, you should set color every time calling bindViewHolder, So try to do like this,
if(color < bracket.getId()) {
textView.setBackgroundResource(R.color.black); // suggests level is locked
} else {
textView.setBackgroundResource(R.color.defaultcolor); // default color
}
this will work fine.
Upvotes: 2