Skip to content
DeveloperMemos

How to Use LazyColumn in Jetpack Compose

Jetpack Compose, LazyColumn, List1 min read

Jetpack Compose is a modern toolkit for building native Android UIs. It simplifies and accelerates UI development with declarative and reactive programming. One of the common UI components that you will need in your app is a list of items that can be scrolled vertically or horizontally. In this article, I will show you how to use LazyColumn in Jetpack Compose to display a vertical list of items efficiently.

What is LazyColumn?

LazyColumn is a composable that displays a vertical list of items that can be scrolled. It is similar to RecyclerView in Android. You can use LazyColumn when you need to display a large number of items or a list of unknown length efficiently.

LazyColumn works by only composing and laying out the visible items on the screen, and recycling them as they scroll off. This way, it avoids unnecessary work and improves performance.

How to Use LazyColumn?

To use LazyColumn, you need to provide a lambda expression that defines the content of each item. You can use different methods on the LazyListScope object inside the lambda expression to add items, such as:

  • items: This method takes a collection of data and a content lambda that defines how each item is displayed.
  • itemsIndexed: This method is similar to items, but it also provides the index of each item in the collection.
  • item: This method adds a single item to the list.
  • stickyHeader: This method adds a header that sticks to the top of the list until it is pushed off by another header.

Here is a code example of how to use LazyColumn:

1// A simple lazy column with 10 items
2LazyColumn {
3 // Add 10 items
4 items(10) { index ->
5 // Content of each item
6 Text(
7 text = "Item $index",
8 modifier = Modifier.padding(16.dp),
9 style = MaterialTheme.typography.h5,
10 color = Color.Black
11 )
12 }
13}

Conclusion

LazyColumn is a powerful and easy-to-use composable that allows you to display a vertical list of items efficiently. You can customize your list with different methods on the LazyListScope object inside the content lambda. You can also use LazyRow for horizontal lists.