tezvyn:

How do you display a large list efficiently in Jetpack Compose?

Curated by the Tezvyn teamSource: developer.android.comintermediate
How do you display a large list efficiently in Jetpack Compose?

This tests understanding of Compose lazy versus eager layout. A strong answer names LazyColumn or LazyRow, notes visible-only composition, and contrasts Column which lays out everything upfront.

WHAT THIS TESTS: Whether the candidate understands the difference between eager and lazy composition in Jetpack Compose, specifically how viewport recycling prevents linear composition and memory costs in large lists. Interviewers want to see that you know when to reach for LazyColumn or LazyRow instead of a standard Column, and that you understand the underlying measurement and layout behavior.

A GOOD ANSWER COVERS: First, name the correct composables which are LazyColumn for vertical lists and LazyRow for horizontal lists. Second, explain the core mechanism which is that lazy containers only compose items currently in or near the viewport using a recycled item pool, while a Column with a loop composes every child eagerly during the composition phase. Third, mention that lazy lists perform lazy measurement and layout so offscreen items do not participate in layout passes, whereas Column measures all children even if they are scrolled away. Fourth, note the importance of providing stable keys via the key parameter in items blocks so Compose can identify individual items across recompositions and enable efficient recycling. Fifth, briefly mention that lazy lists offer built-in semantics and scrolling logic that Column lacks.

COMMON WRONG ANSWERS: Suggesting Column with a for loop and claiming it is fine for a few hundred items without acknowledging the composition cliff. Saying RecyclerView is the only answer and ignoring Compose native lazy lists. Confusing LazyColumn with Column and stating both recycle views, which is false because Column is a simple layout that holds all composables in memory. Forgetting to mention keys and implying that lazy lists magically handle all performance issues without proper item identity.

LIKELY FOLLOW-UPS: How would you handle varying item heights or widths in a lazy list. What is the role of rememberLazyListState and how would you implement scroll position restoration. How do you optimize recompositions inside lazy list items using stable classes or immutable data. When would you choose LazyVerticalGrid over LazyColumn. How does paging or windowed loading integrate with a lazy list.

ONE CONCRETE EXAMPLE: Imagine displaying a chat history with ten thousand messages. Using Column would force Compose to instantiate ten thousand MessageCard composables, allocate corresponding Modifier chains, and retain them in memory even when scrolled offscreen, leading to jank on first render and out of memory crashes. Using LazyColumn with items indexed by messageId as keys composes only the fifteen or twenty messages visible on a modern phone screen plus a small buffer, reusing the same composition slots as the user scrolls, keeping memory flat and frame times under sixteen milliseconds.

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How do you display a large list efficiently in Jetpack Compose? · Tezvyn