RecyclerView变化数据集 [英] RecyclerView change data set

查看:274
本文介绍了RecyclerView变化数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现搜索功能为我的RecyclerView。在文本改变了我想要更改显示这个插件的数据。也许这个问题已经被问过或者是简单的,但我不知道怎么改,它是数据显示...

I want to implement search functionality for my RecyclerView. On text changed i want to change the data that are displayed with this widget. Maybe this question has been asked before or is simple, but I don't know how the change the data that is to be shown...

我RecyclerView定义如下:

My RecyclerView is defined as follows:

     // 1. get a reference to recyclerView
    mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);

    // 2. set layoutManger
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    // 3. create an adapter
    mAdapter = new ItemsAdapter(itemsData);
    // 4. set adapter
    mRecyclerView.setAdapter(mAdapter);

和我正在显示的数据是这样的:

And the data that I am showing is something like:

   ItemData itemsData[] = { new ItemData("Mary Richards"),
        new ItemData("Tom Brown"),
        new ItemData("Lucy London")          
};

所以当我想给适配器另一组数据,另一个阵列(例如一个项目),我该怎么办?

So when when I want to give the adapter another set of data, another array (with one item for example), what should I do?

推荐答案

如果你在你的适配器稳定的ID,就可以得到pretty的好成绩(动画),如果您创建一个包含过滤项目和调用一个新的数组

If you have stable ids in your adapter, you can get pretty good results (animations) if you create a new array containing the filtered items and call

recyclerView.swapAdapter(newAdapter, false);

使用<一个href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html#swapAdapter(android.support.v7.widget.RecyclerView.Adapter,%20boolean)">swapAdapter提示RecyclerView,它可以重复使用视图持有人。 (与在setAdapter,它具有回收所有视图和重新创建,因为它不知道新的适配器具有相同ViewHolder设置与旧适配器)。

Using swapAdapter hints RecyclerView that it can re-use view holders. (vs in setAdapter, it has to recycle all views and re-create because it does not know that the new adapter has the same ViewHolder set with the old adapter).

有一个更好的办法是找出哪些项目将被删除,并呼吁 notifyItemRemoved(指数)。不要忘记真正删除该项目。这将让RecyclerView运行predictive动画。假设你有一个内部使用的一个ArrayList的适配器,实现应该是这样的:

A better approach would be finding which items are removed and calling notifyItemRemoved(index). Don't forget to actually remove the item. This will let RecyclerView run predictive animations. Assuming you have an Adapter that internally uses an ArrayList, implementation would look like this:

// adapter code
final List<ItemData> mItems = new ArrayList(); //contains your items
public void filterOut(String filter) {
   final int size = mItems.size();
   for(int i = size - 1; i>= 0; i--) {
       if (mItems.get(i).test(filter) == false) {
           mItems.remove(i);
           notifyItemRemoved(i);
       }
   }
}

这将执行甚至更好,如果你可以批量 notifyItemRemoved 通话,并使用 notifyItemRangeRemoved 代替。它看起来像某事(未测试)

It would perform even better if you can batch notifyItemRemoved calls and use notifyItemRangeRemoved instead. It would look sth like: (not tested)

public void filterOut(String filter) {
   final int size = mItems.size();
   int batchCount = 0; // continuous # of items that are being removed
   for(int i = size - 1; i>= 0; i--) {
       if (mItems.get(i).test(filter) == false) {
           mItems.remove(i);
           batchCount ++;
       } else if (batchCount != 0) { // dispatch batch
           notifyItemRangeRemoved(i + 1, batchCount);
           batchCount = 0;
       }
   }
   // notify for remaining
   if (batchCount != 0) { // dispatch remaining
       notifyItemRangeRemoved(0, batchCount);
   }
}

您需要扩展这个code补充说,为previously过滤掉的项目,但现在应该是可见的(例如,用户删除过滤查询),但我觉得这个应该了解基本的概念。

You need to extend this code to add items that were previously filtered out but now should be visible (e.g. user deletes the filter query) but I think this one should give the basic idea.

请记住,每个通知项调用所影响的那些后(这就是为什么我遍历从最终名单,以避免它)。从最终穿越还有助于ArrayList中的删除方法性能(较少的项目转移)。

Keep in mind that, each notify item call affects the ones after it (which is why I'm traversing the list from end to avoid it). Traversing from end also helps ArrayList's remove method performance (less items to shift).

例如,如果你遍历从一开始的列表,删除前两个项目。 您应该调用

For example, if you were traversing the list from the beginning and remove the first two items. You should either call

notifyItemRangeRemoved(0, 2); // 2 items starting from index 0

如果你派遣逐一

or if you dispatch them one by one

notifyItemRemoved(0);
notifyItemRemoved(0);//because after the previous one is removed, this item is at position 0

这篇关于RecyclerView变化数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆