如何实时更新RecyclerView? [英] How can I update RecyclerView in real time?
问题描述
我有一个列表,任何用户都可以随时更新.该列表由 RecyclerView 显示.我想实时更新此列表,以便用户获得更好的用户体验,因为用户无需执行多次刷新.现在的问题是,如何实时更新此列表?
I have a list which can be updated any time by any user. The list is showed by in a RecyclerView. I want to update this list in real time so that user can get a better user experience because user need not to perform refresh several times. Now question is, how can I update this list in real time ?
推荐答案
使用RecyclerView,您可以使用适配器更新单个项目,项目范围或整个列表.
With RecyclerView you can either update a single item, item range or the whole list with the adapter.
您必须使用RecyclerView
适配器的方法,例如adapter#notifyItem***
或adapter#notifyDataSetChanged()
(这将更新RecyclerView中的整个项目列表).
You have to use RecyclerView
adapter's method like adapter#notifyItem***
or adapter#notifyDataSetChanged()
(this will update the whole list of items in the RecyclerView).
class RVAdapter extends ...{
List<?> itemList;
public void updateList (List<Object> items) {
if (items != null && items.size() > 0) {
itemList.clear();
itemList.addAll(items);
notifyDataSetChanged();
}
}
}
这篇关于如何实时更新RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!