【发布时间】:2016-05-28 02:04:34
【问题描述】:
RecyclerView 有点问题。
关于 OnBindViewHolder 的 Adapter 方法我正在做一个测试,以定义 RecyclerView 的项目是否应该使用自定义背景颜色。
到目前为止一切正常,问题是,在使用带有SearchView 的过滤器后,如果我使用自定义背景颜色过滤一个结果,在清洁过滤器/折叠SearchView 后,所有其他项目的背景颜色相同。
这是来自我的适配器的一些代码:
@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
}
【问题讨论】:
-
我认为您的问题出在这行代码中 .. holder.layout.setBackgroundResource(R.drawable.recycler_view_selector_colored_item);因为它将颜色设置为整个布局(无论布局是什么)
-
这个
layout是一个RelativeLayout,其中包含该项目的TextViews。不过我现在就去研究一下,谢谢你的回答。 -
不,没有用...
标签: android arraylist filter android-recyclerview searchview