Build: Simple List Screen
This screen shows a scrollable list of 20 items using LazyColumn. Each item displays its number.
Target UI
Simple List Screen ------------------ | Item 1 | | Item 2 | | Item 3 | | ... | | Item 20 | ------------------
Simple List Screen ------------------ | Item 1 | | Item 2 | | Item 3 | | ... | | Item 20 | ------------------
import androidx.compose.foundation.layout.padding 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.unit.dp @Composable fun SimpleListScreen() { // TODO: Add LazyColumn here to show list of items }
import androidx.compose.foundation.layout.padding 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.unit.dp @Composable fun SimpleListScreen() { val itemsList = List(20) { index -> "Item ${index + 1}" } LazyColumn { items(itemsList) { item -> Text( text = item, modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.bodyLarge ) } } }
We create a list of 20 strings labeled "Item 1" to "Item 20" using List constructor. Then, we use LazyColumn to display the list vertically and scrollable. The items() function iterates over the list and shows each item as a Text composable with padding for spacing. This approach is efficient because LazyColumn only renders visible items, making scrolling smooth even for large lists.
Simple List Screen ------------------ | Item 1 | | Item 2 | | Item 3 | | Item 4 | | Item 5 | | Item 6 | | Item 7 | | Item 8 | | Item 9 | | Item 10 | | Item 11 | | Item 12 | | Item 13 | | Item 14 | | Item 15 | | Item 16 | | Item 17 | | Item 18 | | Item 19 | | Item 20 | ------------------
LazyColumn in Android Jetpack Compose?items using LazyColumn?items, not item, itemsList, or list.it and displays it with Text(it).val names = listOf("Anna", "Bob", "Cara")
LazyColumn {
items(names) { name ->
Text(text = name)
}
}val numbers = listOf(1, 2, 3)
LazyColumn {
items(numbers) { number ->
Text(number)
}
}number is an Int, causing a type mismatch.Text(number.toString()) to convert the number to a String before displaying.item { Text("Users") } to add a single header before the list.items(userList) { user -> Text(user) } to display each user below the header.Pass the test and this pattern moves unknown