Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView Adapter notifyDataSetChanged not working

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?

like image 957
David Avatar asked Dec 30 '14 23:12

David


People also ask

What does notifyDataSetChanged do in Recyclerview?

notifyDataSetChanged. Notify any registered observers that the data set has changed. There are two different classes of data change events, item changes and structural changes. Item changes are when a single item has its data updated but no positional changes have occurred.

What is notifyDataSetChanged in Android?

notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.

What is notifyDataSetChanged?

So, that is where notifyDatasetChanged() comes in. It tells the ListView that the data has been modified; and to show the new data, the ListView must be redrawn. Follow this answer to receive notifications.


1 Answers

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

If you prefer declaring it in xml, so you cannot forget it in the code you can also add these lines to the xml of your recyclerView:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" android:orientation="vertical" 
like image 167
Varvara Kalinina Avatar answered Sep 28 '22 21:09

Varvara Kalinina