androidx.compose.foundation.lazy
LazyColumn
Composable function · from org.jetbrains.compose.foundation:foundation:1.12.2
A LazyColumn is a vertical list that scrolls. It is lazy: it only creates the items you can see, and a few more. A list with 10 000 items is as fast as one with 10.
Unlike a Column, you don't call composables directly inside. You describe the items with item { } and items(list) { }:
LazyColumn(
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
item { Text("Messages", style = MaterialTheme.typography.titleLarge) }
items(messages, key = { it.id }) { message ->
ListItem(headlineContent = { Text(message.subject) })
}
}
The content lambda runs in a LazyListScope:
item { }: one item, like a header.items(list) { element -> }: one item per element of a list.items(count) { index -> }: a number of items.itemsIndexed(list) { index, element -> }: with the position.stickyHeader { }: a header that stays at the top while its section scrolls.
Give items a key, like an ID. Then the list keeps each item's state and scroll position right when items are added, removed or moved.
To scroll from code or read the scroll position, pass a state from rememberLazyListState(). Its animateScrollToItem(index) is a suspend function.
Don't put a LazyColumn inside a Column with verticalScroll: both want to scroll, and it crashes. Put all content in the one LazyColumn instead.
Parameters
@Composable
fun LazyColumn(
modifier: Modifier = Modifier,
state: LazyListState = rememberLazyListState(),
contentPadding: PaddingValues = PaddingValues(0.dp),
reverseLayout: Boolean = false,
verticalArrangement: Arrangement.Vertical = if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,
horizontalAlignment: Alignment.Horizontal = Alignment.Start,
flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior(),
userScrollEnabled: Boolean = true,
overscrollEffect: OverscrollEffect? = rememberOverscrollEffect(),
content: LazyListScope.() -> Unit,
)
modifier: ModifierOptional
modifier: ModifierOptionalDefault: Modifier
Changes size, padding, or position. See the Modifier guide.
state: LazyListStateOptional
state: LazyListStateOptionalDefault: rememberLazyListState()
Reads and controls the scroll position. Create it with rememberLazyListState.
contentPadding: PaddingValuesOptional
contentPadding: PaddingValuesOptionalDefault: PaddingValues(0.dp)
Space around all items, inside the scroll area. Items scroll through it, so the first and last items are not cut off. Better than Modifier.padding for lists.
reverseLayout: BooleanOptional
reverseLayout: BooleanOptionalDefault: false
Set to true to start at the bottom, like a chat. Items are still given in order; the first one is at the bottom.
verticalArrangement: Arrangement.VerticalOptional
verticalArrangement: Arrangement.VerticalOptionalDefault: if (!reverseLayout) Arrangement.Top else Arrangement.Bottom
The space between items: Arrangement.spacedBy(8.dp).
horizontalAlignment: Alignment.HorizontalOptional
horizontalAlignment: Alignment.HorizontalOptionalDefault: Alignment.Start
How items narrower than the list line up: start, center or end.
flingBehavior: FlingBehaviorOptional
flingBehavior: FlingBehaviorOptionalDefault: ScrollableDefaults.flingBehavior()
How the list slows down after a swipe. To snap to items, use rememberSnapFlingBehavior(state).
userScrollEnabled: BooleanOptional
userScrollEnabled: BooleanOptionalDefault: true
Set to false to stop the user from scrolling. You can still scroll from code.
overscrollEffect: OverscrollEffect?OptionalSince 1.8
overscrollEffect: OverscrollEffect?OptionalSince 1.8Default: rememberOverscrollEffect()
What shows when the user scrolls past the end, like a stretch. Pass null for none.
content: LazyListScope.() -> UnitRequired
content: LazyListScope.() -> UnitRequiredThe items: calls to item, items and stickyHeader.
Longer Example
A contact list grouped by first letter. It uses most of what lazy lists offer:
stickyHeaderfor the letters, so the current letter stays at the top.items(..., key = { it.id })so each row keeps its state when the list changes.Modifier.animateItem()so rows slide into place when items are added or removed.- A
LazyListState, to scroll back to the top from a button. derivedStateOf, so the button only recomposes when it should appear or disappear, not on every scrolled pixel. See the State guide.

val sampleContacts = listOf(
val listState = rememberLazyListState()
val scope = rememberCoroutineScope()
// Group once, not on every recomposition.
val groups = remember { sampleContacts.groupBy { it.name.first() } }
Box(Modifier.size(360.dp, 420.dp)) {
LazyColumn(state = listState, contentPadding = PaddingValues(bottom = 80.dp)) {
groups.forEach { (letter, contacts) ->
stickyHeader(key = "header-$letter") {
Text(
letter.toString(),
Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surfaceContainer)
.padding(horizontal = 16.dp, vertical = 4.dp),
style = MaterialTheme.typography.titleSmall,
)
}
items(contacts, key = { it.id }) { contact ->
ListItem(
headlineContent = { Text(contact.name) },
leadingContent = { Avatar(contact.name) },
// animateItem moves the row smoothly when items are added, removed or reordered.
modifier = Modifier.animateItem(),
)
}
}
}
// Shown once the first item has scrolled away. derivedStateOf avoids recomposing on every pixel.
val showTop by remember { derivedStateOf { listState.firstVisibleItemIndex > 0 } }
if (showTop) {
SmallFloatingActionButton(
onClick = { scope.launch { listState.animateScrollToItem(0) } },
modifier = Modifier.align(Alignment.BottomEnd).padding(16.dp),
) { Icon(Icons.Filled.KeyboardArrowUp, "Back to top") }
}
}
/** The data of one row. A data class with a stable id, used as the list key. */
data class Contact(val id: Int, val name: String, val email: String, val favorite: Boolean = false)
private val sampleContacts = listOf(
Contact(1, "Ada Lovelace", "[email protected]", favorite = true),
Contact(2, "Alan Turing", "[email protected]"),
Contact(3, "Barbara Liskov", "[email protected]"),
Contact(4, "Grace Hopper", "[email protected]", favorite = true),
Contact(5, "Katherine Johnson", "[email protected]"),
Contact(6, "Linus Torvalds", "[email protected]"),
)
/** A round avatar with the first letter of the name. */
@Composable
private fun Avatar(name: String) {
Box(
Modifier.size(40.dp).background(MaterialTheme.colorScheme.primaryContainer, CircleShape),
contentAlignment = Alignment.Center,
) { Text(name.take(1), color = MaterialTheme.colorScheme.onPrimaryContainer) }
}
List types
LazyColumnLazyRow