0

I have a RecyclerView with a GridLayoutManager and a custom GridAdapter that just handles styling, sizing, etc. It does not include any animations.

I'm observing a strange effect with the built-in RecyclerView animations, and I'm wondering if I'm missing something.

When inserting new items / deleting items and calling notifyDataSetChanged(), if the deleted / new item will be fully on-screen, a slide-up animation is played. However, if the item is at all off-screen, a pop animation is played. I would expect the slide-up animation to be played whenever an item is added or deleted, and at all visible.

Here's a video of the effect, it's too large to embed: https://i.imgur.com/jK4QM77.mp4

The RecyclerView has:

setHasFixedSize(true)
setItemViewCacheSize(12)

and the adapter has:

setHasStableIds(true)

All animations / animation handling is the built in RecyclerView default, any ideas what's going on? Thank you!

Jake Lee
  • 7,549
  • 8
  • 45
  • 86
  • Try using adapter.notifyItemInserted() and adapter.notifyItemRemoved() methods instead of adapter.notifyDataSetChanged() methods. These are both better for performance and for getting correct animations. – Oya Canli Jul 08 '21 at 10:39
  • And I'm a bit confused by the video, because contents don't seem to be changed when you delete or insert items. And you said GridLayoutManager but it seems to behave like a LinearLayoutManager (as whole row is added or deleted at once) – Oya Canli Jul 08 '21 at 10:41
  • @OyaCanli Sorry, video is a little misleading. The contents ARE changed, but titles are updated (e.g. if inserting before "Pages 2 & 3", they become "Pages 3 & 4" (hence needing to update other cells, although I could do update range). Also, each row is two cells, that are inserted / deleted together. – Jake Lee Jul 08 '21 at 11:30
  • Oh I see.. The behavior you want is normally provided by predictive animations, which is enabled by default. I wonder if predictive animations doesn't work correctly for GridLayoutManager for some reason.. – Oya Canli Jul 08 '21 at 13:17
  • 1
    Here is a related question: https://stackoverflow.com/q/29177931/9758208 (not clear whether answers helped) – Oya Canli Jul 08 '21 at 13:46
  • Ah, a potential (very involved) [solution for gridlayoutmanager](https://wiresareobsolete.com/2015/02/recyclerview-layoutmanager-3/) is linked from there, thanks! – Jake Lee Jul 08 '21 at 14:16

0 Answers0