items und itemsIndexed
Rendern Sie eine Sammlung von Datenzeilen.
items und itemsIndexed ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
From Data to Rows
Your data usually lives in a list, like a List of strings or objects. The items block turns each element into a row on screen.
The items Function
Call items with your collection and a lambda. Compose runs the lambda for each element it needs to show.
LazyColumn {
items(fruits) { fruit ->
Text(fruit)
}
}The Element Parameter
The lambda receives one element at a time. Name it clearly, like fruit or user, then build UI from that single value.
items(users) { user ->
Text(user.name)
}Build a Whole Row
The lambda can hold any layout, not just Text. Wrap several composables in a Row to make a richer list item.
items(users) { user ->
Row { Text(user.name); Text(user.age.toString()) }
}When You Need the Position
Sometimes you want the row's index, like showing a rank number or alternating colors. Plain items doesn't give you that.
Meet itemsIndexed
Use itemsIndexed when you need the position. Its lambda receives both the index and the element together.
itemsIndexed(songs) { index, song ->
Text("$index: $song")
}Index Comes First
Order matters: in itemsIndexed the index is the first parameter and the element is the second. Swapping them is a common slip.
itemsIndexed(tasks) { i, task ->
Text("Step ${i + 1}: $task")
}Numbering Made Easy
Index starts at zero, so add one for human-friendly numbering. This makes ranked or step-by-step lists feel natural.
Add a Single Fixed Row
Need one standalone row, like a header or footer? Use item (singular) for a single piece of content among your items blocks.
LazyColumn {
item { Text("My List") }
items(data) { Text(it) }
}Mix and Match Freely
Inside one LazyColumn you can combine several item and items calls. They render in the order you declare them.
Pick the Right Tool
Reach for items when the element is enough, and itemsIndexed when you also need each row's position. That's the whole choice.
Quick Check
Pick the right builder for the job.
Recap: items and itemsIndexed
Use items to map data to rows, itemsIndexed when you need the position, and item for a single fixed row. Mix them freely. 🎯
Häufig gestellte Fragen
Ist die Lektion „items und itemsIndexed“ kostenlos?
Ja — der vollständige Text von „items und itemsIndexed“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „items und itemsIndexed“?
Rendern Sie eine Sammlung von Datenzeilen. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „items und itemsIndexed“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- LazyColumn vs. scrollbare Column
- items und itemsIndexed
- Keys für stabile, schnelle Listen
- Fixierte Überschriften und Abschnittslisten