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:
Any ideas?
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.
notifyDataSetChanged. Notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
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.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With