Skip to content
DeveloperMemos

Using itemsIndexed in Jetpack Compose

Jetpack Compose, Android Development, Kotlin2 min read

Jetpack Compose is a modern UI toolkit for building Android applications. It provides a declarative way of creating user interfaces using composable functions. One common requirement when working with lists or RecyclerViews is the need to access the index of each item in the list. This can be useful for various scenarios, such as tracking item positions, handling click events, or applying different styling based on the index. In this article, we will learn how to utilize the itemsIndexed function in Jetpack Compose to achieve this.

Using itemsIndexed Function

The itemsIndexed function is a useful extension function provided by Jetpack Compose that allows you to iterate over a list while also providing the index of each item. It is similar to the forEachIndexed function in Kotlin collections.

To use itemsIndexed, you need to have a list of items and a composable function that takes the index and item as parameters. Here's an example:

1@Composable
2fun MyList(items: List<String>) {
3 LazyColumn {
4 itemsIndexed(items) { index, item ->
5 Text("Item at index $index: $item")
6 }
7 }
8}

In the above example, we have a LazyColumn composable, which is a vertically scrolling list component in Jetpack Compose. We pass the items list to the itemsIndexed function, and inside the lambda, we have access to the index and item for each iteration. We use this information to display the item with its corresponding index using the Text composable.

Example: Highlighting Even and Odd Items

Let's consider a scenario where we want to highlight even and odd items in a list with different background colors. We can achieve this by using the itemsIndexed function and conditional styling based on the index.

1@Composable
2fun MyList(items: List<String>) {
3 LazyColumn {
4 itemsIndexed(items) { index, item ->
5 val backgroundColor = if (index % 2 == 0) {
6 Color.LightGray
7 } else {
8 Color.White
9 }
10
11 Box(
12 modifier = Modifier
13 .background(color = backgroundColor)
14 .fillMaxWidth()
15 .padding(16.dp)
16 ) {
17 Text("Item at index $index: $item")
18 }
19 }
20 }
21}

In this example, we calculate the backgroundColor based on the index. If the index is even, we set it to Color.LightGray; otherwise, we set it to Color.White. We apply this background color to a Box composable, which wraps the Text composable representing each item in the list. Additionally, we apply some modifiers to achieve the desired padding and fill the width of the list.

Wrap Up

The itemsIndexed function in Jetpack Compose provides a convenient way to iterate over a list while also accessing the index of each item. This can be useful for various scenarios where you need to work with the index, such as applying conditional styling, handling click events, or tracking item positions. By leveraging itemsindexed, you can enhance the functionality and flexibility of your Jetpack Compose UIs.

Now that you have a solid understanding of how to use itemsIndexed in Jetpack Compose, go ahead and experiment with it in your own projects. Harness the power of declarative UIs and leverage the index information to create dynamic and engaging user interfaces!