清除搜索视图时如何刷新我的 RecyclerView 列表?
How to refresh my RecyclerView list when searchview is cleared?
输入搜索查询文本后,我会在 RecyclerView
中过滤我的卡片项目并将它们显示在顶部。然而,当所有文本被清除后,由于某种原因,列表不会回到原来的状态。如何刷新我的 RecyclerView
列表?下面的代码应该会给你一个更好的主意。
@Override
public boolean onQueryTextChange(String newText) {
final List<Number> filteredModelList = filter(numbers, newText);
Log.v("App", newText + ", " + numbers.size() + ", " + filteredModelList.size());
adapter.animateTo(filteredModelList);
list.scrollToPosition(0);
adapter.notifyDataSetChanged();
if (newText.equalsIgnoreCase("")){
try{
Log.e("SEARCH VIEW", "I CAN ENTER HERE"); //But the list doesn't refresh
adapter.animateTo(numbers);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
list.scrollToPosition
} catch(Exception e){
Log.e("Caught Exception", e.toString());
}
}
return true;
}
当没有项目与搜索匹配时,这是有问题的 - 然后屏幕只是空白,没有卡片,SearchView
上的 "X" 按钮也不可见。当 X 可见并且我触摸它时,列表会返回到其原始状态。作为第二个问题,有没有办法始终显示关闭图标 ("X")?
当您在数组上应用过滤器时,仅搜索数组中可用的记录然后您清除时间数组没有值的搜索文本...
所以你制作一个临时数组并存储原始数组并对临时数组应用过滤器并添加到主数组并设置为列表视图当你删除所有搜索文本时在主数组中添加临时数组并设置为适配器。
注意:- 始终对 tamp 数组应用过滤器并将结果添加到主数组并设置为适配器
希望对你有所帮助....
记住你有一个将数据加载到 RecyclerView 的方法,所以你所做的,你在 IF 语句中再次调用该方法,例如:当 newText
length 等于 equal 到 zero
,做点什么......
算法如下:.
if (newText.length() == 0){
//Reload the data Again , use the existing method you have of loading data in the RecyclerView .
}
如果您遇到一些问题,只需 post 再次将您的代码加载到 RecyclerView
中,我会再次向您展示。
@Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){ //Show all data
adapter.animateTo(numbers);
adapter.notifyDataSetChanged();
return ;
}
//Do Filter
}
输入搜索查询文本后,我会在 RecyclerView
中过滤我的卡片项目并将它们显示在顶部。然而,当所有文本被清除后,由于某种原因,列表不会回到原来的状态。如何刷新我的 RecyclerView
列表?下面的代码应该会给你一个更好的主意。
@Override
public boolean onQueryTextChange(String newText) {
final List<Number> filteredModelList = filter(numbers, newText);
Log.v("App", newText + ", " + numbers.size() + ", " + filteredModelList.size());
adapter.animateTo(filteredModelList);
list.scrollToPosition(0);
adapter.notifyDataSetChanged();
if (newText.equalsIgnoreCase("")){
try{
Log.e("SEARCH VIEW", "I CAN ENTER HERE"); //But the list doesn't refresh
adapter.animateTo(numbers);
adapter.notifyDataSetChanged();
list.setAdapter(adapter);
list.scrollToPosition
} catch(Exception e){
Log.e("Caught Exception", e.toString());
}
}
return true;
}
当没有项目与搜索匹配时,这是有问题的 - 然后屏幕只是空白,没有卡片,SearchView
上的 "X" 按钮也不可见。当 X 可见并且我触摸它时,列表会返回到其原始状态。作为第二个问题,有没有办法始终显示关闭图标 ("X")?
当您在数组上应用过滤器时,仅搜索数组中可用的记录然后您清除时间数组没有值的搜索文本...
所以你制作一个临时数组并存储原始数组并对临时数组应用过滤器并添加到主数组并设置为列表视图当你删除所有搜索文本时在主数组中添加临时数组并设置为适配器。
注意:- 始终对 tamp 数组应用过滤器并将结果添加到主数组并设置为适配器
希望对你有所帮助....
记住你有一个将数据加载到 RecyclerView 的方法,所以你所做的,你在 IF 语句中再次调用该方法,例如:当 newText
length 等于 equal 到 zero
,做点什么......
算法如下:.
if (newText.length() == 0){
//Reload the data Again , use the existing method you have of loading data in the RecyclerView .
}
如果您遇到一些问题,只需 post 再次将您的代码加载到 RecyclerView
中,我会再次向您展示。
@Override
public boolean onQueryTextChange(String newText) {
if(TextUtils.isEmpty(newText)){ //Show all data
adapter.animateTo(numbers);
adapter.notifyDataSetChanged();
return ;
}
//Do Filter
}