Mastering RecyclerView Animations for Android

Watch this great video to understand how RecyclerView animations work on Android and what are the different components of the RecyclerView. It is really useful if you want to implement your custom item animations.

P.S.

For a list of item animations, check out this cool GITHUB repo.

Here are the notes I made

RecyclerView Animations and Behind the Scenes (Android Dev Summit 2015)

RecyclerView Components

  • Layout Manager
  • Item Animator
  • Adapter
  • Item Decorations
  • Recycled View Pool
  • Child Helper – handles all the incoming and outgoing views
  • Adapter Helper – handles adapter updates
  • Recycler

LayoutManager

  1. Positioning – A simple component that positions your items. Like a list, grid.
  2. Scrolling is its main responsibility
  3. Part of the focus remains to the RecyclerView. If the user pushes down then the LayoutManager responsibility to move the other items out and bring new view and focus on it.
  4. Accessibility – use a delegate to notify the RecyclerView which data should not be displayed

Adapter

  1. Creates view and ViewHolder
  2. Bind an item to ViewHolder
  3. Notify RecyclerView about changes
  4. Notify data out of sync
  5. Handle Click handling
  6. Handle multiple view types
  7. Recycler recovery
  8. Single item change

Adapter Lifecycle

ItemDecoration

  • Custom drawing on RecyclerView canvas
  • Add offset to view bounds
  • Multiple ItemDecorations

Take care

  • Do not try to access the adapter
  • Keep necessary information in ViewHolder
  • General onDraw rules apply
  • recyclerView.getChildViewHolder(view) to get the ViewHolder

RecyclerViewPool

Useful when you use a RecyclerViews inside a RecyclerView

  • Sanctuary for reserve ViewHolders
  • Can be shared between RecyclerView or custom ViewGroups
  • Per Activity context

RecyclerView Animations

Predictive Animations

Help the LayoutManager of the RecyclerView to keep a virtual list of the items. So when you add or remove an item the fade of the last element in the list will be displayed properly. Check THIS part of the video.

Important things to know

  • Items are not rebound unless updated
  • Do not make position final in onBind calls
  • Use onCreateViewHolder with holder.getAdapterPosition instead in onBind
  • Use item payloads to tell what changed in an item
  • getAdapterPosition vs getLayoutPosition – you can change the order of the list of data but the layout position stays the same
  • Always create view holder, do not cache it
  • Do not make a single instance of the RecycledViewPool
    • You will leak the context
    • RecyclerView.context and ItemView.context differ
  • Do not make change animations on another thread
  • Do not update the content on another thread. Update the adapter on the main thread.

You may also like...