items e itemsIndexed
Visualizzi una raccolta di righe di dati.
items e itemsIndexed è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎯
Domande Frequenti
La lezione «items e itemsIndexed» è gratuita?
Sì — il testo completo di «items e itemsIndexed» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Cosa imparerò in «items e itemsIndexed»?
Visualizzi una raccolta di righe di dati. Eserciti Jetpack Compose Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Jetpack Compose Academy?
Non è richiesta alcuna esperienza precedente. Jetpack Compose Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «items e itemsIndexed»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Jetpack Compose Academy?
Sì. Ogni lezione Jetpack Compose Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- LazyColumn e Column scorrevole a confronto
- items e itemsIndexed
- Chiavi per liste stabili e veloci
- Intestazioni fisse e liste per sezioni