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.

What was checked, and how: read this first
Checked in the docs (one web search, summaries only): 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).

import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyRow import androidx.compose.foundation.lazy.items import androidx.compose.material3.Card import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.unit.dp
@Composable fun NameList(names: List<String>) { LazyColumn( contentPadding = PaddingValues(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp) ) { items(names) { name -> Card(modifier = Modifier.fillMaxWidth()) { Text(text = name, modifier = Modifier.padding(16.dp)) } } } } @Preview(showBackground = true) @Composable fun NameListPreview() { ComposeHelloTheme { NameList(listOf("Ada", "Grace", "Alan", "Katherine")) } }
PartWhat 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.

import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue
data class Contact(val id: Int, val name: String) @Composable fun ContactRow(contact: Contact, selected: Boolean, onClick: () -> Unit) { Card( modifier = Modifier .fillMaxWidth() .clickable(onClick = onClick) ) { Text( text = if (selected) "${contact.name} (selected)" else contact.name, modifier = Modifier.padding(16.dp) ) } } @Composable fun ContactListScreen(contacts: List<Contact>) { var selectedId by rememberSaveable { mutableStateOf<Int?>(null) } LazyColumn( contentPadding = PaddingValues(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp) ) { items(contacts, key = { it.id }) { contact -> ContactRow( contact = contact, selected = contact.id == selectedId, onClick = { selectedId = contact.id } ) } } }

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.

@Composable fun EditableList() { val people = remember { mutableStateListOf(Contact(1, "Ada"), Contact(2, "Grace")) } var nextId by remember { mutableIntStateOf(3) } Column { Button(onClick = { people.add(Contact(nextId, "Person $nextId")); nextId++ }) { Text("Add") } LazyColumn { items(people, key = { it.id }) { person -> Row(verticalAlignment = Alignment.CenterVertically) { Text(person.name, modifier = Modifier.weight(1f).padding(8.dp)) Button(onClick = { people.remove(person) }) { Text("Remove") } } } } } }

LazyRow and List State

@Composable fun TagRow(tags: List<String>) { LazyRow( contentPadding = PaddingValues(horizontal = 16.dp), horizontalArrangement = Arrangement.spacedBy(8.dp) ) { items(tags) { tag -> Card { Text(text = tag, modifier = Modifier.padding(12.dp)) } } } }

(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).

val listState = rememberLazyListState() LazyColumn(state = listState) { /* items */ } // Read where the user has scrolled to val firstVisible = listState.firstVisibleItemIndex
A common crash
(general knowledge, not checked) Do not put a 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 dataYou pass a list to items(...)
A ViewHolder holds one row's viewsA composable function draws one row; there are no views to hold
An item layout XML fileThe lambda, or a row composable
DiffUtil finds what changedRecomposition plus key
An item click listener set in the adapterAn onClick lambda passed to the row
LayoutManager (linear, grid)LazyColumn, LazyRow and lazy grids (grids not covered here)
Compared with the Classic UI course
Android Development Fundamentals 5 teaches the adapter pattern, ViewHolder, DiffUtil and click handling in RecyclerView. For a Compose app none of that machinery is needed: one composable per row, a key for identity, and state for changes. RecyclerView is still used in older apps (Chapter 9 covers working beside it).

Try It Yourself

Exercise 1

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.

๐Ÿ“„ View solution
Exercise 2

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?

๐Ÿ“„ View solution
Exercise 3

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.

๐Ÿ“„ View solution
Not Yet Verified
Checked in the docs (one web search, summaries only): 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).
Coming up
Chapter 7 covers Navigation Compose: NavHost, routes, arguments, the back stack, bottom navigation and deep links. It is how a tapped row opens a detail screen.