[Go Back]
Android Recycler View Lifecycle diagrams and notes
Main Components
Main RecyclerView components.
ViewHolder Life Cycle
- While Layout Manager is calculating layout, it asks RecyclerView for View objects
recyclerView.getViewForPosition(...)
- RecylcerView checks the Cache for a hit
cache.getViewForPosition(...)
- If in Cache, return to RecyclerView
- RecyclerView returns to LayoutManager
- While Layout Manager is calculating layout, it asks RecyclerView for View objects
recyclerView.getViewForPosition()
- RecylcerView checks the Cache for a hit
cache.getViewForPosition()
- Cache does not have a view
- RecyclerView asks adapter for View Type
adapter.getViewType()
- RecyclerView asks Pool for ViewHolder for type
pool.getViewHolderByType()
- Pool does not have match
- RecyclerView asks Adapter to create ViewHolder
adapter.createViewHolder()
- While Layout Manager is calculating layout, it asks RecyclerView for View objects
<
recyclerView.getViewForPosition()
- RecylcerView checks the Cache for a hit
cache.getViewForPosition()
- Cache does not have a view
- RecyclerView asks adapter for View Type
adapter.getViewType()
- RecyclerView asks Pool for ViewHolder for type
pool.getViewHolderByType()
- Pool returns match
- RecyclerView binds data
adapter.bindViewHolder()
- Recycler returns View to Layout Manager
- Layout Manager tells RecyclerView the view has been added to layout
addView
- RecyclerView tells adapter
onViewAttachedToWindow
- Layout needs to remove a view, view is recycled.
removeAndRecyclerView()
- Tell Adapter to recycle view
onViewDetachedFromWindow()
- If view is valid for position, tell cache to hold iten.
- Cache tells Pool if any older items are discarded.
- Pool tells Adapter if any items are discarded.
ViewHolder can discard its contents to save memory.
- Layout Manager does not need all of the views.
For example item is deleted
- Each unused view is added to hidden adapter list
this allows itemAnimator to perform animations on them such as fade out.
- When itemAnimator is done, it tells Adapter
onViewDetachedFromWindow
- Causing recycler to update cache and recycle unused item.
When layout does not need view, and the view has a transient state (such as fading out) it can't be reused, so ask adapter.
onFailedToRecyle()
Adapter can reset the state of the ViewHolder and return it or not.
Pool has a fixed size per type and if full, the viewHolder will be discarded.
Generally happens if you animate all items, cause it doubles the number of items to do a cross fade.
ItemAnimator
Default animations:
Action | Animation
|
Item Removed | Fade Out
|
Item Added | Fade In
|
Item Moved | Translate position
|
Changing | Cross-fade
|
Child Helper
Child helper decides which views to return to layout to support conditions such as add or remove by managing an alternate adapter which may have items which are in the process of being added or delete and the animation will fade them in or out.
Adapter Helper
Adapter helper keeps a virtual list of add and remove positions to help compute correct items in list.
Item Decorations
RecycledViewPool
Drag and Drop
Item Touch Helper
Tips and Tricks