Lists: LazyColumn, LazyRow and LazyVerticalGrid
LazyColumn
LazyColumn is the Compose replacement for RecyclerView. It creates only the rows that are visible.
kotlin
data class Fruit(val id: Int, val name: String, val emoji: String)
@Composable
fun FruitList(fruits: List<Fruit>, modifier: Modifier = Modifier) {
LazyColumn(
modifier = modifier,
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items(fruits, key = { it.id }) { fruit -> // key keeps state correct when the list changes
Card(Modifier.fillMaxWidth()) {
Row(Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) {
Text(fruit.emoji, fontSize = 32.sp)
Spacer(Modifier.width(16.dp))
Text(fruit.name, style = MaterialTheme.typography.titleMedium)
}
}
}
}
}
Always give a key: A stable
key lets Compose know which row is which when items move or are removed. Without it, row state and animations can jump to the wrong item.
Headers, Footers and Mixed Content
kotlin
LazyColumn {
item { Text("Header", style = MaterialTheme.typography.headlineSmall) }
stickyHeader { Text("A", Modifier.fillMaxWidth().background(Color.LightGray).padding(8.dp)) }
items(listOf("Apple", "Avocado")) { Text(it, Modifier.padding(16.dp)) }
itemsIndexed(listOf("x", "y")) { index, value -> Text("$index: $value") }
}
LazyRow Carousel
kotlin
LazyRow(
contentPadding = PaddingValues(horizontal = 16.dp),
horizontalArrangement = Arrangement.spacedBy(12.dp)
) {
items((1..10).toList()) { i ->
Box(
Modifier.size(140.dp, 180.dp).background(Color(0xFFFFA726), RoundedCornerShape(16.dp)),
contentAlignment = Alignment.Center
) { Text("$i", fontSize = 32.sp, color = Color.White) }
}
}
Grid
kotlin
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 100.dp), // as many columns as fit
contentPadding = PaddingValues(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
items((1..30).toList()) { i ->
Box(
Modifier.aspectRatio(1f).background(Color.hsv(i * 12f, 0.5f, 0.9f)),
contentAlignment = Alignment.Center
) { Text("$i") }
}
}
| GridCells | Behavior |
|---|---|
Fixed(3) | Exactly 3 columns |
Adaptive(100.dp) | As many columns as fit, each at least 100.dp |
Scroll Position and Buttons
kotlin
val listState = rememberLazyListState()
val scope = rememberCoroutineScope()
LazyColumn(state = listState) { /* items */ }
Button(onClick = { scope.launch { listState.animateScrollToItem(0) } }) { Text("Top") }
val showButton by remember { derivedStateOf { listState.firstVisibleItemIndex > 3 } }
Swipe to Delete
kotlin
items(tasks, key = { it.id }) { task ->
val dismissState = rememberSwipeToDismissBoxState(
confirmValueChange = {
if (it == SwipeToDismissBoxValue.EndToStart) { onDelete(task); true } else false
}
)
SwipeToDismissBox(
state = dismissState,
enableDismissFromStartToEnd = false,
backgroundContent = {
Box(Modifier.fillMaxSize().background(Color.Red).padding(16.dp), contentAlignment = Alignment.CenterEnd) {
Icon(Icons.Default.Delete, contentDescription = "Delete", tint = Color.White)
}
}
) { TaskRow(task) }
}
Performance: Do not put a LazyColumn inside a vertically scrolling Column. Use one LazyColumn with several
item blocks instead.
Conclusion
Lazy lists render only what is visible. Always add keys, use contentPadding and combine item blocks for mixed content.
Quick Summary: LazyColumn and LazyRow for lists, LazyVerticalGrid for grids, key for stable rows and LazyListState for scrolling.

