I'm having a little problem with a RecyclerView
.
On the method OnBindViewHolder
of it's Adapter
I'm making a test, to define or not if the item of the RecyclerView
should use a custom background color.
Up to this point everything is working fine, the problem is, after using a filter with a SearchView
if I filter the one result with the custom background color, after cleaning the filter/collapse the SearchView
, all other items are getting the same background color.
Here is some code from my adapter:
@Override
public void onBindViewHolder (MyAdapter.VieHolder holder, int position){
holder.textViewName.setText(myVisibleList.get(position).getName());
// myVisibleList is the list that is show in the recyclerView
// If true, is birthday, then change the item background color
if(myVisibleList.get(position).isBirthDay){
holder.layout.setBackgroundResource(R.drawable.recycler_view_selector_colored_item);
}
}
@Override
public Filter getFilter(){return null;}
public void setFilter(String query){
myVisibleList = new ArrayList<>();
// Contact is the object show in the recyclerView / allContacts is the list if all the contacts
for (Contact c : allContacts){
if(c.getName().contains(query)){
myVisibleList.add(c);
}
}
notifyDataSetChanged();
}
// Method to clear the filter / Called when the searchView is closed
public void clearFilter(){
myVisibleList = new ArrayList<>();
myVisibleList.addAll(allContacts);
notifyDataSetChanged
}
I think that you need to add an else clause here:
if (myVisibleList.get(position).isBirthDay){
holder.layout.setBackgroundResource(R.drawable.recycler_view_selector_colored_item);
} else {
holder.layout.setBackgroundResource(R.drawable.my_default_recycler_view_color_item);
// or holder.layout.setBackgroundResource(0);
}
See docs where it says that
0 [is] to remove the background
Hope this helps...