items และ itemsIndexed
แสดงแถวชุดข้อมูล
items และ itemsIndexed เป็นบทเรียน Jetpack Compose Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Jetpack Compose Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
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. 🎯
คำถามที่พบบ่อย
บทเรียน “items และ itemsIndexed” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “items และ itemsIndexed” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Jetpack Compose Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Jetpack Compose Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “items และ itemsIndexed”
แสดงแถวชุดข้อมูล คุณปฏิบัติ Jetpack Compose Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Jetpack Compose Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Jetpack Compose Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “items และ itemsIndexed” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Jetpack Compose Academy นี้ได้ไหม
ได้ บทเรียน Jetpack Compose Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- LazyColumn กับคอลัมน์แบบเลื่อนได้
- items และ itemsIndexed
- คีย์สำหรับรายการที่เสถียรและรวดเร็ว
- ส่วนหัวติดด้านบนและรายการแบ่งส่วน