【问题标题】:Android recyclerview filter showing no items until searchview icon is clicked在单击 searchview 图标之前,Android recyclerview 过滤器不显示任何项目
【发布时间】:2016-03-08 11:14:42
【问题描述】:

我有一个片段,我在其中进行网络调用并用卡片填充包含的回收器视图。现在我跟着this 为我的回收站视图添加了一个过滤器。现在我在适配器构造函数中出现问题

this.storeLists = new ArrayList<>(storeLists);

我的片段中的列表被填充但不显示任何项目,直到我单击搜索视图图标并开始输入内容。之后,即使我关闭搜索视图,列表也会保留。

我尝试将上面的行更改为

this.storeLists = storeLists;

当我删除搜索查询时,已删除的项目不会重新显示。因此,如果我在回收站视图列表中搜索不存在的内容,我的回收站视图中将一无所有。

我想要的是,当我打开片段时,我会看到结果,然后当我单击搜索图标并开始输入过滤器时,过滤器的工作方式如上面的链接所示。

编辑

Here 是我的适配器,here 是我的调用片段。

【问题讨论】:

  • 我能理解你的问题是什么,我也面临同样的问题。如果你给我看你的代码,我会更容易找到问题
  • @RageshRamesh 编辑并添加了代码。

标签: android android-fragments filter android-recyclerview android-search


【解决方案1】:

您正在使用 this.storeLists = new ArrayList(storeLists);因此 notifyDatasetChanged 将无法正常工作,因为您完全使用的是新列表。

但您不应该更改它,因为您需要保留 storeLists,因为它包含您的完整数据。您可以做的是在您的适配器中创建如下方法

 public void setList(ArrayList<StoreList> mList){
   this.storeList = mList;
   notifyDataSetChanged();
 }

在你的片段中,一旦你下载了数据并添加到你的商店列表中,调用 setList 方法如下

 mAdapter.setList(storeList);

同时删除 storeReq 方法中的 mAdapter.notifyDataSetChanged(),因为它什么都不做。

【讨论】:

【解决方案2】:

经过一番尝试和尝试,我找到了以酷动画为代价的解决方案。现在,代码段只需像这样重置适配器即可工作:

@Override
public boolean onQueryTextChange(String newText) {
    Log.d(TAG,newText);
    searchStoreList = storeList;
    final List<StoreList> filteredModelList = filter(searchStoreList, newText);
    //((RVStoreAdapter) mAdapter).animateTo(filteredModelList);
    //The above commented line is the old code. Following is how to reset the adapter!
    mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new RVStoreAdapter(getActivity(),storeList,session.getLat(),session.getLongi());
    mRecyclerView.setAdapter(mAdapter);
    mRecyclerView.scrollToPosition(0);
    return true;
}

动画现在非常突然,但解决方案会在相应地更改搜索查询时重置列表。

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2021-01-30
    • 2021-10-21
    相关资源
    最近更新 更多