RecyclerView, DiffUtil & XML Layouts
UI › Views & RecyclerView
RecyclerView as an abstraction, DiffUtil and ItemDecoration, and flat, fast XML layouts.
View-based live-coding rounds use RecyclerView to test whether you understand it as an abstraction rather than boilerplate: what the Adapter, ViewHolder, LayoutManager and RecycledViewPool each own, how recycling actually works, and why DiffUtil beats notifyDataSetChanged(). Expect follow-ups on layout performance: deep nesting, double taxation, and using ItemDecoration instead of baking spacing into item XML. Interviewers watch whether your bind code stays cheap and allocation-free.
What this covers
- RecyclerView's collaborators: the Adapter creates and binds ViewHolders, the LayoutManager measures and positions children, and the RecycledViewPool stores scrapped holders per view type for reuse
- Recycling means rebinding: offscreen ViewHolders are scrapped and rebound with new data, so inflation (expensive) happens rarely and onBindViewHolder (cheap) happens constantly
- notifyDataSetChanged() is a last resort: it rebinds everything and kills item animations; DiffUtil / ListAdapter compute minimal insert, move and change notifications
- DiffUtil's contract: areItemsTheSame (identity), areContentsTheSame (equality), getChangePayload (partial rebind); ListAdapter diffs on a background thread
- ItemDecoration adds spacing, dividers and headers via getItemOffsets and onDraw, keeping layout concerns out of the item XML and reusable across lists
- Flat layouts win: deep nesting and nested LinearLayout weights multiply measure passes (double taxation); flatten with ConstraintLayout, <merge> and ViewStub
- onCreateViewHolder does one-time setup (inflation, click listeners); onBindViewHolder must stay cheap: no allocations, no listener churn, guard NO_POSITION
Study this topic
- RecyclerView, DiffUtil & XML Layouts explained: the guided lesson
- 14 practice quiz questions
- 9 revision flashcards
- RecyclerView, DiffUtil & XML Layouts interview questions and answers