RecyclerView Adapter notifyDataSetChanged not working

David picture David · Dec 31, 2014 · Viewed 40.7k times · Source

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?

Answer

Varvara Kalinina picture Varvara Kalinina · Oct 24, 2016

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