Reputation: 1204
I am trying to highlight the active row of recyclerview which is playing in my music player.
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
viewHolder.txt1.setText(data.get(i).get("text1").toString());
viewHolder.txt2.setText(data.get(i).get("text2").toString());
try{
if(data.get(i).get("text1").toString().equals("Alan Walker - Alone")){
viewHolder.txt1.setTextColor(context.getColor(R.color.activeText));
}
}catch (Exception e){}
}
In the following example data is just a list of songs. I am just trying to select "Alan Walker - Alone" but the problem is
some other rows are highlighted too.. any solutions?
Upvotes: 0
Views: 48
Reputation: 20714
RecyclerView recycles old items to create new items. So whenever you use an if
condition in onBindViewHolder()
, you must also use an else
.
So your code becomes like this:
if(data.get(i).get("text1").toString().equals("Alan Walker - Alone")){
viewHolder.txt1.setTextColor(context.getColor(R.color.activeText));
} else {
viewHolder.txt1.setTextColor(/*Default text color*/);
}
Upvotes: 3