RecyclerView Adapter notifyDataSetChanged not working

半城伤御伤魂 提交于 2019-11-27 03:45:30

问题


I extended

RecyclerView.Adapter<RecyclerView.ViewHolder>

And when I called:

mRecyclerView.getAdapter().notifyDataSetChanged();

Nothing happened.

The only way to refresh the view is to set again the adapter (see this answer):

mRecyclerView.setAdapter(new MyAdapter(...));

I have two issues with this solution:

  1. I can see a blink on the screen when I set again the adapter
  2. The listview returns to the first position.

Any ideas?


回答1:


If notifyDataSetChanged() does not trigger view updates than there is a chance that you have forgotten to call SetLayoutManager() on your RecyclerView (like I did!). Just don't forget to do this: Java code:

LinearLayoutManager layoutManager = new LinearLayoutManager(context ,LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager)

C# code, I'm using Xamarin.

var layoutManager = new LinearLayoutManager(Context, LinearLayoutManager.Vertical, false);
recyclerView.SetLayoutManager(layoutManager);

before you call recyclerView.SetAdapter(adapter);




回答2:


If your getItemCount() returns 0, then notifyDataSetChanged() won't do anything. Make sure that when you initialize your adapter, you are passing a valid dataset.




回答3:


According to the javadocs: If you are writing an adapter it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort.

public class NewsAdapter extends RecyclerView.Adapter<...> {    

private static List mFeedsList;
...

    public void swap(List list){
    if (mFeedsList != null) {
        mFeedsList.clear();
        mFeedsList.addAll(list);
    }
    else {
        mFeedsList = list;
    }
    notifyDataSetChanged();
}

I am using Retrofit to fetch the list, on Retrofit's onResponse() use,

adapter.swap(feedList);



回答4:


To update recyclerview we can do the following:

  1. Create and set adapter again:

    adapter=new MyAdapter(...);
    mRecyclerView.setAdapter(adapter);
    
  2. Clear model list data and then notify:

    List<YourModel> tempModel=new ArrayList<>(modelList); 
    modelList.clear();   
    modelList.addAll(tempModel); 
    adapter.notifyDataSetChanged();
    



回答5:


In my case, nothing but this has worked. When it's time to refresh a RecyclerView, I did:

array = getNewItems();                    // populates the list with new items
((MyAdapter) mAdapter).setValues(array);  // pass the new list to adapter !!!
mAdapter.notifyDataSetChanged();          // tell the adapter a data set has changed

In the MyAdapter, I put the setter for data:

public void setValues(List<Item> items){
    this.items = items;
}

That's it!

[Notice that the notifyDataSetChanged does not work without setting new values to adapter.]




回答6:


Want to share something, I was facing the same issue. But what i was doing wrong was. I was creating instance of Adapter every time new and than doing notifysetDatachange() to that new instance not the older one.

So please make sure Adapter whom you notifysetDatachange() should be older one. Hope below example helps..

     MyAdapter mAdapter = new MyAdapter(...)

    mRecyclerView.setAdapter(mAdapter );

// TODO 
mAdapter.modifyData(....);
mAdapter.notifySetDataChange();


    MyAdapter extends baseAdapter {
     MyAdapter () {
    }
    modifyData(String[] listData) {

    }

    }



回答7:


So i fixed my problem like this : orderList is the List that i pass on to the recyclerview . We can just add the item at the position in the list, here 0 is the 0th position in the List . Then call adapter.notifyDataSetChanged() . Works like a charm

1) orderList.add(0,String); 2) orderAdapter.notifyDataSetChanged();




回答8:


notifyDataSetChanged() sholud be called in main thread.



来源:https://stackoverflow.com/questions/27714127/recyclerview-adapter-notifydatasetchanged-not-working

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