RecyclerView适配器notifyDataSetChanges()不刷新recyclerview

时间:2016-12-29 14:25:53

标签: android android-recyclerview recycler-adapter notifydatasetchanged

我已经设置了像这样的RecyclerView

@Override
public void setTenData(List<Data> dataList) {
    Timber.d("set ten drugs size %s",dataList.size());
        this.dataList = dataList;
        dataListAdapter = new DataListingAdapter(getActivity(), this.dataList);
        mRecyclerView.setAdapter(dataListAdapter);

}

另一种方法是我获得新数据,当我尝试像这样刷新RecyclerView时

@Override
public void setNewsData(List<Data> dataList) {
    this.dataList = dataList;
    Timber.d("size of news data %s",this.dataList.size());
    dataListAdapter.notifyDataSetChanged();
}

RecyclerView没有更新。可能是什么问题?

4 个答案:

答案 0 :(得分:2)

您需要在适配器中设置新数据,而不仅仅是在视图中

答案 1 :(得分:0)

@Override
public void setTenData(List<Data> dataList) {
    Timber.d("set ten drugs size %s",dataList.size());
        this.dataList = dataList;
        dataListAdapter = new DataListingAdapter(getActivity(), this.dataList);
        mRecyclerView.setAdapter(dataListAdapter);
dataListAdapter.notifyDataSetChanged();

}

答案 2 :(得分:0)

您可以简单地清除列表并向其添加新数据,然后通知适配器。在这里我修改了你的代码:

@Override
public void setNewsData(List<Data> dataList) {
    this.dataList.clear();
    this.dataList.addAll(dataList);
    dataListAdapter.notifyDataSetChanged();
}

答案 3 :(得分:0)

    public void setNewsData(List<Data> dataList) {
    this.dataList.addAll(dataList); //        if datalist has only new data
    //this.dataList = dataList;//        if datalist has all data
    Timber.d("size of news data %s",this.dataList.size());
}

然后调用主线程;

dataListAdapter = (RecyclerViewAdapter) recyclerView.getAdapter();
dataListAdapter.setNewsData(dataList);
dataListAdapter.notifyDataSetChanged();