How can I update RecyclerView in real time?

随声附和 提交于 2019-12-07 11:40:33

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

}

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.

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

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