Using Notifyitemremoved or Notifydatasetchanged with Recyclerview in Android

using notifyItemRemoved or notifyDataSetChanged with RecyclerView in Android

As @pskink suggested it was supposed to be (index+1) in my case with notifyItemRemoved(index+1), probably because i am reserving the top index i.e. position=0 for a header.

RecyclerView onClick notifyItemRemoved doesn't trigger onBindView

If you are trying to remove an item from RecyclerView Adapter and want to show animation all over your list in RecyclerView.

after using notifyItemRemoved(position) use notifyItemRangeChanged(position, getItemCount());

notifyItemRemoved(position); - notifies the RecyclerView Adapter that data in adapter has been removed at a particular position.

notifyItemRangeChanged(position, getItemCount()); - notifies the RecyclerView Adapter that positions of element in adapter has been
changed from position(removed element index to end of list), please
update it.

Refer this RecyclerView insert /remove animation answer.

RecyclerView duplicating items after deletion from contextual action mode

The problem was happening because variable

private List<FavoritesEntity> favoritesList;

inside FavouritesFragment which kept all previous objects even after deleting items from a local database.

On every delete event, you were getting updates about database change -> postViewModel.metal favorites().observe, where you called addAll to this reference which contained all previous items.

Solution: https://github.com/dautovicharis/DummyApp2/commit/752a48fc98761d53c6a8f72076489a0f68ee348b

Source: https://github.com/dautovicharis/DummyApp2

How to properly notifyItemMoved, insert and removed in RecyclerView old data list when i get new fresh list to assign to it

It looks like that your new data is also a form of list, not a single item. I think this could be a good candidate for using DiffUtil in the support library.
Here is also a nice tutorial for it.

It will allow you to calculate the difference in the new data and only update needed fields. It will also offload the work asynchronously.

You just need to implement a DiffUtil.Callback to indicate if your items are the same or the contents are the same.

You update your recyclerView like that:

DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallback);
diffResult.dispatchUpdatesTo(yourAdapter);


Related Topics



Leave a reply



Submit