0
0
Android Kotlinmobile~5 mins

LazyColumn with items in Android Kotlin

Choose your learning style9 modes available
Introduction

LazyColumn helps you show a long list of items efficiently by loading only what is visible on screen.

Displaying a list of messages in a chat app.
Showing a list of contacts or friends.
Listing products in a shopping app.
Displaying search results that can be very long.
Syntax
Android Kotlin
LazyColumn {
    items(list) { item ->
        Text(text = item)
    }
}

LazyColumn creates a vertical scrolling list.

items() takes a list and a lambda to show each item.

Examples
Shows a list of fruit names in a scrollable column.
Android Kotlin
val fruits = listOf("Apple", "Banana", "Cherry")

LazyColumn {
    items(fruits) { fruit ->
        Text(text = fruit)
    }
}
Shows 5 items labeled from Item #0 to Item #4.
Android Kotlin
LazyColumn {
    items(5) { index ->
        Text(text = "Item #$index")
    }
}
Sample App

This program shows a vertical scrollable list of fruit names using LazyColumn and items.

Android Kotlin
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun FruitList() {
    val fruits = listOf("Apple", "Banana", "Cherry", "Date", "Elderberry")
    LazyColumn(modifier = Modifier.fillMaxSize()) {
        items(fruits) { fruit ->
            Text(text = fruit, style = MaterialTheme.typography.bodyLarge)
        }
    }
}

@Preview(showBackground = true)
@Composable
fun FruitListPreview() {
    Surface {
        FruitList()
    }
}
OutputSuccess
Important Notes

LazyColumn only creates items when they appear on screen, saving memory.

You can customize each item with any composable, not just Text.

Remember to import androidx.compose.foundation.lazy.items to use the items function.

Summary

LazyColumn shows vertical scrolling lists efficiently.

Use items() to display each element from a list.

It helps keep your app smooth with large lists.