RecyclerView Internals
Problem Explain the internal working of Android's RecyclerView.
Be ready to discuss
- The component split: Adapter creates and binds ViewHolders, LayoutManager positions and measures items (linear, grid, staggered), ItemAnimator animates changes, ItemDecoration draws dividers and offsets.
- View recycling: views scrolling off-screen are detached into a pool and rebound for incoming items instead of being inflated again - the core performance win over ListView.
- The ViewHolder pattern: each item view caches its child view references, so
findViewByIdruns once per holder rather than on every bind. - The cache hierarchy: attached scrap (attached but being rebound in the current layout pass), the cached views list (recently detached, rebound with no
onCreateViewHolder), the ViewCacheExtension hook, and the RecycledViewPool (keyed by view type, shareable across RecyclerViews). - Why
onCreateViewHolderis expensive andonBindViewHolderis cheap, and why keeping bind work trivial keeps scrolling at 60fps. notifyDataSetChangedvs granularnotifyItemInserted/Removed/Changed: granular calls preserve holders and let the ItemAnimator animate only what moved.- DiffUtil and ListAdapter: computing a minimal changeset off the main thread and dispatching granular notifications automatically.
- Multiple view types via
getItemViewType, and how the pool partitions by type.
asked …