我在我的Recycler视图上实现了searchView过滤器。现在我需要找到我的列表何时被过滤以及何时不被过滤。
这是我的自定义适配器中的可过滤实现:
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
dataList = (List<ProductLight>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<ProductLight> FilteredArrayNames = new ArrayList<ProductLight>();
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < dataListFilter.size(); i++) {
ProductLight dataNames = dataListFilter.get(i);
if (dataNames.getTitle().toLowerCase().startsWith(constraint.toString())){
FilteredArrayNames.add(dataNames);
}
}
results.count = FilteredArrayNames.size();
results.values = FilteredArrayNames;
return results;
}
};
return filter;
}
在我的Fragment中(在optionMenu中有searchview)我正在过滤我的列表:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu, menu);
MenuItem item = menu.findItem(R.id.search_menu);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
isListFiltered = true ;
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
});
现在我不知道在哪里将isListFiltered
切换为false。