[Go Back]

Android Recycler View Lifecycle diagrams and notes

The following images are captured from the presentation 'RecyclerView ins and outs- Google IO 2016'
By: Adam Powell and Yigit Boyar

Main Components

Main RecyclerView components. parts


ViewHolder Life Cycle

  1. While Layout Manager is calculating layout, it asks RecyclerView for View objects
    recyclerView.getViewForPosition(...)
  2. RecylcerView checks the Cache for a hit
    cache.getViewForPosition(...)
  3. If in Cache, return to RecyclerView
  4. RecyclerView returns to LayoutManager


  1. While Layout Manager is calculating layout, it asks RecyclerView for View objects
    recyclerView.getViewForPosition()
  2. RecylcerView checks the Cache for a hit
    cache.getViewForPosition()
  3. Cache does not have a view
  4. RecyclerView asks adapter for View Type
    adapter.getViewType()
  5. RecyclerView asks Pool for ViewHolder for type
    pool.getViewHolderByType()
  6. Pool does not have match
  7. RecyclerView asks Adapter to create ViewHolder
    adapter.createViewHolder()
viewholder2


  1. While Layout Manager is calculating layout, it asks RecyclerView for View objects <
    recyclerView.getViewForPosition()
  2. RecylcerView checks the Cache for a hit
    cache.getViewForPosition()
  3. Cache does not have a view
  4. RecyclerView asks adapter for View Type
    adapter.getViewType()
  5. RecyclerView asks Pool for ViewHolder for type
    pool.getViewHolderByType()
  6. Pool returns match
  7. RecyclerView binds data
    adapter.bindViewHolder()
  8. Recycler returns View to Layout Manager
viewholder3


  1. Layout Manager tells RecyclerView the view has been added to layout
    addView
  2. RecyclerView tells adapter
    onViewAttachedToWindow
viewholder4


viewholder5

  1. Layout needs to remove a view, view is recycled.
    removeAndRecyclerView()
  2. Tell Adapter to recycle view
    onViewDetachedFromWindow()
  3. If view is valid for position, tell cache to hold iten.
  4. Cache tells Pool if any older items are discarded.
  5. Pool tells Adapter if any items are discarded.
    ViewHolder can discard its contents to save memory.
viewholder6


  1. Layout Manager does not need all of the views.
    For example item is deleted
  2. Each unused view is added to hidden adapter list
    this allows itemAnimator to perform animations on them such as fade out.
  3. When itemAnimator is done, it tells Adapter
    onViewDetachedFromWindow
  4. Causing recycler to update cache and recycle unused item.
viewholder7


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. viewholder8


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. viewholder9


ItemAnimator

Default animations:
Action Animation
Item Removed Fade Out
Item Added Fade In
Item Moved Translate position
Changing Cross-fade


Child Helper

childhelper1

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.

childhelper2


Adapter Helper

adapterhelper1

Adapter helper keeps a virtual list of add and remove positions to help compute correct items in list.


Item Decorations

decoration


RecycledViewPool

viewpool


Drag and Drop

Item Touch Helper

itemtouchhelper itemtouchhelper


Tips and Tricks