使用filterable在recyclerview中搜索效果很好,但是我在recyclerview中的项目上有三个已经选择的项目视图,然后在recyclerview过滤器中搜索结果很好,但是所选项目视图出现在未选中的项目上。
例如:我在recyclerview中有三个选定的项目(facebook,instagram,youtube),当我搜索snapchat时,然后在列表中未选定的Snapchat上显示了选定的项目视图。
这里是过滤器功能
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String query = charSequence.toString();
List<AddNetworkFileModel> filtered = new ArrayList<>();
if (query.isEmpty()) {
filtered = mCategory;
} else {
for (AddNetworkFileModel networkFileModel : mCategory) {
if (networkFileModel.getmNetworkTitle().toLowerCase().contains(query.toLowerCase())) {
filtered.add(networkFileModel);
}
}
mFilterNetworkList=filtered;
}
FilterResults results = new FilterResults();
results.count = filtered.size();
results.values = filtered;
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults results) {
mFilterNetworkList = (ArrayList<AddNetworkFileModel>) results.values;
for(int i=0;i<mFilterNetworkList.size();i++) {
Log.e(LOG_TAG, "publishResults: " + mFilterNetworkList.get(i).getmNetworkTitle());
}
notifyDataSetChanged();
}
};
}
适配器
private class SocialNetworksAdapter extends RecyclerView.Adapter<SocialNetworksViewHolder> implements Filterable {
private List<AddNetworkFileModel> mCategory;
private List<AddNetworkFileModel> mFilterNetworkList;
SocialNetworksAdapter(List<AddNetworkFileModel> network){
mCategory = network;
mFilterNetworkList=network;
}
@Override
public int getItemCount() {
return mFilterNetworkList.size();
}
@Override
public void onBindViewHolder(final SocialNetworksViewHolder holder, final int position) {
try {
final AddNetworkFileModel category = mFilterNetworkList.get(position);
holder.bindData(category);
// that's function show the already selected item view on the recyclerview item mFirebaseDatabase.child(userId).child("SocialNetworks").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot updateSnapshot : dataSnapshot.getChildren()) {
AddNetworkFileModel social = updateSnapshot.getValue(AddNetworkFileModel.class);
if (mFilterNetworkList.size() != 0) {
if (social != null) {
if (social.getmNetworkTitle().toUpperCase().equals(category.getmNetworkTitle().toUpperCase())) {
holder.delete.setVisibility(View.VISIBLE);
}
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(LOG_TAG, "onCancelled: " + databaseError.toString());
}
}); }