Lists with LazyColumn
Android Development Fundamentals with Compose
Course 4 ยท Chapter 6 ยท Showing a List of Things
Almost every app shows a list: contacts, messages, products, items in a catalogue. In the Views world that meant a RecyclerView with an adapter, a ViewHolder and a DiffUtil. In Compose it is a LazyColumn and a lambda that says how to draw one row.
This chapter shows that, then how to give rows stable identity with keys, how to react to a tap on a row, and how to scroll sideways with LazyRow.
contentPadding (padding on the content, not the list), Arrangement.spacedBy for gaps, key in items() to keep per-item state when items move, and LazyListState (scroll position and scrolling methods).
Not found in the docs search: click handling on rows (the clickable approach is general knowledge), LazyRow's own parameters beyond the horizontal arrangement note, and the full items and itemsIndexed signatures. All code is unrun (no Kotlin compiler on the machine used to write this).
Nothing was seen on your install.
A First LazyColumn
(docs) A lazy list only creates the rows that are on screen (or about to be), which is why a list of ten thousand items is not a problem. You describe the rows inside the LazyColumn block with items.
A Column with a forEach would build every row at once and cannot scroll by itself (general knowledge).
| Part | What it does |
|---|---|
items(names) { name -> ... } | Calls the lambda once per element, to describe that row. |
contentPadding | (docs) Padding applied to the content rather than the list container, so rows scroll past the edges instead of being clipped. |
Arrangement.spacedBy(8.dp) | (docs) A gap between rows. A LazyRow uses horizontalArrangement the same way. |
Keys
(docs) Without a key, an item is identified by its position, so when rows move, any state remembered inside them goes to the wrong row. Pass a stable ID (not the position) through the key lambda. The foundation
release notes add that Compose can keep the scroll position tied to a key when items are added or removed above the visible one. In the example below the ID is the contact's id.
Rows, Selection and Taps
(general knowledge, not found by my search) Write one row as its own composable. Make it stateless (Chapter 4): it receives the data, whether it is selected and an onClick, and the screen owns the state. Make the row tappable with Modifier.clickable.
Tapping a row sets selectedId; the list recomposes and the chosen row shows "(selected)". The row never owns the selection, so it is easy to preview and reuse.
Changing the List
A list you add to or remove from must be state, or the UI will not follow. mutableStateListOf (Chapter 4) gives that. Keys matter here: with keys, removing the second row does not disturb the others.
LazyRow and List State
(docs) LazyListState holds the scroll position, exposes firstVisibleItemIndex and firstVisibleItemScrollOffset, and has methods such as animateScrollToItem (run inside a coroutine) for scrolling by code. Create it with rememberLazyListState() (general knowledge for that function name).
LazyColumn inside a Column that scrolls vertically itself (Modifier.verticalScroll): a scrolling parent gives its child unlimited height, and Compose reports an error. Make the list the scrolling element, and put other content above it as a normal child or as extra items.
Compared with RecyclerView
| RecyclerView (Android Development Fundamentals) | Compose |
|---|---|
| An Adapter supplies the data | You pass a list to items(...) |
| A ViewHolder holds one row's views | A composable function draws one row; there are no views to hold |
| An item layout XML file | The lambda, or a row composable |
| DiffUtil finds what changed | Recomposition plus key |
| An item click listener set in the adapter | An onClick lambda passed to the row |
| LayoutManager (linear, grid) | LazyColumn, LazyRow and lazy grids (grids not covered here) |
Try It Yourself
Show a list of 30 names in cards using LazyColumn (generate them with List(30) { "Person $it" }). Add content padding and spacing, and add a preview.
Using the Contact data class, build ContactListScreen so tapping a row selects it. Then add a "Clear selection" button above the list. Which composable owns the selection?
Build EditableList: a button that adds a person and a Remove button on each row. Run it with and without the key argument and describe any difference you can see. Then add a LazyRow of tags above the list.
contentPadding applying to content; Arrangement.spacedBy and horizontalArrangement for LazyRow; key in items(); scroll position kept by key; LazyListState, firstVisibleItemIndex, firstVisibleItemScrollOffset, animateScrollToItem.Not found in the docs search: row click handling,
rememberLazyListState by name, the scrolling-parent crash and the "Column builds everything" remark (general knowledge); the RecyclerView comparison column.Not verified: the imports; whether keyed and unkeyed lists differ visibly in Exercise 3 (the docs say state follows the key; what you will actually see is for you to report); every listing and solution (unrun); the exercises (not run by me). Nothing was checked on your install.
Sources: Lazy lists and lazy grids (developer.android.com), LazyListScope API reference (developer.android.com), Compose Foundation release notes (developer.android.com).
NavHost, routes, arguments, the back stack, bottom navigation and deep links. It is how a tapped row opens a detail screen.