【问题标题】:How to refresh my RecyclerView list when searchview is cleared?清除搜索视图后如何刷新我的 RecyclerView 列表?
【发布时间】:2016-12-25 07:32:40
【问题描述】:

输入搜索查询文本后,我会过滤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”)?

【问题讨论】:

  • 请发布filteranimateTo方法
  • 嗨,我不允许发布这些方法。但是,与问题一样,我正在输入条件 [f (newText.equalsIgnoreCase("")){] - 我需要在那里刷新我的视图。该怎么做?
  • 所以很难提供帮助。检查您是否没有从这些方法中删除带有 callByRefrence 的项目,检查您是否从带有空字符串的过滤器方法返回正确的数据。您需要对其进行调试以找出问题所在
  • RecyclerView写一个generic适配器(类似于ArrayAdapter为普通ListViews),那么以后就不用写这个过滤了每次需要新的“过滤回收器视图”时,一遍又一遍地编写代码

标签: java android android-recyclerview searchview android-cardview


【解决方案1】:

请记住,您有一个将数据加载到 RecyclerView 中的方法,所以您要在 IF 语句中再次回调该方法,例如:当 newTextlength 等于 到 @ 987654322@,做点什么……

以下是算法:.

if (newText.length() == 0){    
    //Reload the data Again , use the existing  method you have of loading data in the RecyclerView .
}

如果您有问题,只需在RecyclerView 中再次发布您的代码加载数据,我会再次向您展示。

【讨论】:

  • 上述答案的可能编辑:“newText.length”应该是“newText.length()”吗?
【解决方案2】:

当您对数组应用过滤器时,数组中只有搜索记录可用,然后您清除时间数组没有值的搜索文本...

所以你制作一个临时数组并存储原始数组并在临时数组上应用过滤器并添加到主数组并设置为列表视图当你删除所有搜索文本时在主数组中添加临时数组并设置为适配器。

注意:- 始终在 tamp 数组上应用过滤器并将结果添加到主数组并设置为适配器

希望对你有帮助....

【讨论】:

    【解决方案3】:
    @Override
    public boolean onQueryTextChange(String newText) {
        if(TextUtils.isEmpty(newText)){  //Show all data 
          adapter.animateTo(numbers);
          adapter.notifyDataSetChanged();
          return ;
        }
         //Do Filter
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 2011-09-11
      • 2020-07-24
      • 1970-01-01
      相关资源
      最近更新 更多