-1

I have to do the following thing: when a user clicks a button, I need to change the title's color for all cards in RecyclerView. I've been searching and searching and I couldn't find something to help me, except this method: findViewHolderForAdapterPosition(int i). Unfortunately, it returns null (Android recyclerView findViewHolderForAdapterPosition returns null).

Andrei Pascale
  • 232
  • 1
  • 3
  • 16

1 Answers1

2

You can declare a new boolean variable in your adapter, let's call it changeColor initialize it to false on the adapter constructor and then in your onClick action method set it to true and call yourAdapter.notifyDataSetChanged(). Finally add the test to the onBindViewHolder method of your adapter, something like that:

// your onclick method
void onClick() {
 //code..;
 changeColor = true;
 yourAdapter.notifyDataSetChanged();
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
 if(changeColor)
     holder.yourItemTitle.setTextColor(ContextCompat.getColor(context, newTitleColor));
 else
     holder.yourItemTitle.setTextColor(ContextCompat.getColor(context, defaultTitleColor));
}