RecyclerView, DiffUtil & XML Layouts Flashcards

UI › Views & RecyclerView

What do the Adapter, LayoutManager and RecycledViewPool each own in RecyclerView's abstraction?
The Adapter creates ViewHolders and binds data to them; the LayoutManager measures and positions item views (linear, grid, staggered); the RecycledViewPool stores scrapped ViewHolders keyed by view type so they can be reused instead of re-inflated.
What does "recycling" actually mean in RecyclerView?
ViewHolders that scroll offscreen are scrapped into the pool and later rebound with new data via onBindViewHolder(). Inflation only happens when the pool is empty, so the expensive step (inflate) is rare and the cheap step (bind) is constant.
Why is notifyDataSetChanged() a last resort?
It marks every item as invalid: all visible ViewHolders are rebound, view types can't be reused optimally, and item animations are lost. Granular notifications (or DiffUtil) let RecyclerView rebind only what changed and animate moves/inserts.
What do DiffUtil's areItemsTheSame() and areContentsTheSame() each decide?
areItemsTheSame() checks identity (usually stable ids): is this the same logical item? areContentsTheSame() checks equality of displayed content. Same item + different contents produces a change (rebind); different item produces insert/remove/move.
What is getChangePayload() for in DiffUtil?
When an item changed, it can return a payload describing WHAT changed. onBindViewHolder(holder, position, payloads) can then update just that part (e.g. only the like count) instead of rebinding the whole row.
What does ListAdapter add over calling DiffUtil manually?
It wraps AsyncListDiffer: submitList() computes the diff on a background thread and dispatches the granular updates on the main thread. You get correct, non-blocking diffing without writing the threading yourself.
How should item spacing/dividers be implemented, and why not margins in the item XML?
With an ItemDecoration: getItemOffsets() adds space, onDraw() draws dividers. It keeps layout concerns out of the item view, composes (multiple decorations per list), can differ per position, and is reusable across lists.
Why does deep XML nesting hurt performance, and what flattens it?
Every extra level adds measure/layout work, and nested LinearLayout weights cause double taxation (children measured multiple times). Flatten with ConstraintLayout, remove redundant wrappers with <merge>, and defer rarely-shown UI with ViewStub.
What belongs in onCreateViewHolder() vs onBindViewHolder()?
Create: inflation, click listeners, one-time setup. Bind: cheap, allocation-free data updates only. Attach listeners once at create time and read bindingAdapterPosition at click time (guarding NO_POSITION), never bake positions into listeners at bind time.

Back to RecyclerView, DiffUtil & XML Layouts