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.
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)
- Layout Manager
- Item Animator
- Item Decorations
- Recycled View Pool
- Child Helper – handles all the incoming and outgoing views
- Adapter Helper – handles adapter updates
- Positioning – A simple component that positions your items. Like a list, grid.
- Scrolling is its main responsibility
- 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.
- Accessibility – use a delegate to notify the RecyclerView which data should not be displayed
- Creates view and ViewHolder
- Bind an item to ViewHolder
- Notify RecyclerView about changes
- Notify data out of sync
- Handle Click handling
- Handle multiple view types
- Recycler recovery
- Single item change
- Custom drawing on RecyclerView canvas
- Add offset to view bounds
- Multiple ItemDecorations
- Do not try to access the adapter
- Keep necessary information in ViewHolder
- General onDraw rules apply
- recyclerView.getChildViewHolder(view) to get the ViewHolder
Useful when you use a RecyclerViews inside a RecyclerView
- Sanctuary for reserve ViewHolders
- Can be shared between RecyclerView or custom ViewGroups
- Per Activity context
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.