Reputation: 67
I'm implementing a Notepad app using Recyclerview.
When adding, removing and editing note, I will return a newListnote which was modified to display on UI and call adapter.NotifyDataSetChanged().
public static void UpdateUI(List<Entity_NoteItem> newlistNote) {
/*listNoteToDisplayOnUI = newListNote();
adapter.notifyDataSetChanged();*/
adapter = new DisplayUI_CustomIconLabelAdapter(adapter.context, newlistNote);
myRecyclerView.setAdapter(adapter);
}
But I realized that if I assigned the listNote which is set as data of the adapter by a another object with another memory address, the method NotifyDataSetChanged() will have no effect. Therefore, I decided to new adapter with new listNote and called myRecyclerView.setAdapter(adapter) and it gets more effect.
And my question is: Why should we use the method Adapter.NotifyDataSetChanged() while we can completely new Adapter with the changed data and setAdapter again?
Upvotes: 1
Views: 303
Reputation: 3756
Why should we use the method Adapter.NotifyDataSetChanged() while we can completely new Adapter with the changed data and setAdapter again???
When you call setAdapter(...)
, all rows created from scratch. Bu when using notifyDataSetChanged()
, old rows can be re-used. So if you want performance, you should use notifyDataSetChanged()
.
Also RecyclerView.Adapter has these methods too:
notifyItemChanged(int)
notifyItemInserted(int)
notifyItemRemoved(int)
notifyItemRangeChanged(int, int)
notifyItemRangeInserted(int, int)
notifyItemRangeRemoved(int, int)
Upvotes: 4