这是我在“ if(query == null)”中的代码。如您所见,我没有在其中放置任何代码,因为如果用户的输入与searchview不匹配,我不知道如何在RecyclerView中显示“未找到结果”。我正在使用RecyclerView来显示用户的搜索结果。
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_main, menu);
SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_filter_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getActivity().getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(getResources().getString(R.string.search_hint));
// listening to search query text change
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// filter recycler view when query submitted
mAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String query) {
// filter recycler view when text is changed
mAdapter.getFilter().filter(query);
return false;
}
});
super.onCreateOptionsMenu(menu, inflater);
}
在适配器中
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
categoriesFiltered = categorieslist;
} else {
List<CategoriesP> filteredList = new ArrayList<>();
for (CategoriesP row : categorieslist) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
//|| row.getPhone().contains(charSequence)
if (row.getName().toLowerCase().contains(charString.toLowerCase()) ) {
filteredList.add(row);
}
}
categoriesFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = categoriesFiltered;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults filterResults) {
categoriesFiltered = (ArrayList<CategoriesP>) filterResults.values;
notifyDataSetChanged();
}
};
}
答案 0 :(得分:0)
在XML中设置TextView并为此设置行android:visibility="gone"
。
在“活动”中输入此代码...
if (listMesage.isEmpty()) {
mRecyclerMessage.setVisibility(View.GONE);
mTxtEmpty.setVisibility(View.VISIBLE);
}
else {
mRecyclerMessage.setVisibility(View.VISIBLE);
mTxtEmpty.setVisibility(View.GONE);
}