How can I update RecyclerView in real time?

こ雲淡風輕ζ 提交于 2019-12-08 08:43:12

问题


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 ?


回答1:


With RecyclerView you can either update a single item, item range or the whole list with the adapter.

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();
        }
    }

}



回答2:


You can create metod inside your Adapter which updates the data and refreshes ur recyclerview.

class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
   List<Data> data;
   ...

 public void update(ArrayList<Data> datas){
    data.clear();
    data.addAll(datas);
    notifyDataSetChanged();
 }
    ...
}

Just call the update method passing new data. That's it.




回答3:


You can call same set recycler adapter code. do not forget to call adapter#notifyDataSetChanged()



来源:https://stackoverflow.com/questions/40039501/how-can-i-update-recyclerview-in-real-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!