Chỉ định loại nội dung cho từng mục bằng cách đặt contentType trong items().
Ánh xạ từng loại nội dung đến một thành phần kết hợp tương ứng. Ví dụ: Audio là một contentType được xác định ở nơi khác và được liên kết với một thành phần kết hợp AudioMessage.
Compose sử dụng lại các thành phần kết hợp đã kết xuất cho từng mục của một loại nội dung nhất định.
Kết quả
Hình 1. Kết quả mã hiển thị âm thanh và tin nhắn văn bản.
Các bộ sưu tập chứa hướng dẫn này
Hướng dẫn này là một phần của các bộ sưu tập Hướng dẫn nhanh được tuyển chọn này, bao gồm các mục tiêu phát triển Android rộng hơn:
Hiển thị danh sách hoặc lưới
Danh sách và lưới cho phép ứng dụng của bạn hiển thị các bộ sưu tập ở dạng hình ảnh dễ nhìn và dễ sử dụng cho người dùng.
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-02-06 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-02-06 UTC."],[],[],null,["# Build a list using multiple item types\n\n\u003cbr /\u003e\n\nYou can use a list with multiple item types to display mixed content types such\nas text, images, and interactive elements.\n\nVersion compatibility\n---------------------\n\nThis implementation requires that your project minSDK be set to API level 21 or\nhigher.\n\n### Dependencies\n\n### Kotlin\n\n\u003cbr /\u003e\n\n```kotlin\n implementation(platform(\"androidx.compose:compose-bom:2025.08.00\"))\n \n```\n\n\u003cbr /\u003e\n\n### Groovy\n\n\u003cbr /\u003e\n\n```groovy\n implementation platform('androidx.compose:compose-bom:2025.08.00')\n \n```\n\n\u003cbr /\u003e\n\nAdd multiple item types\n-----------------------\n\nYou can specify the content type for each item of the layout when you compose a\nlist or a grid with multiple types of items:\n\n\n```kotlin\n@Composable\nfun ListWithMultipleItems(messages: List\u003cAny\u003e) {\n LazyColumn {\n items(\n messages.size,\n contentType = { it }\n ) {\n for (message in messages)\n when (message) {\n is MediaStore.Audio -\u003e AudioMessage(message)\n is Text -\u003e TextMessage(message)\n }\n }\n }\n}\n\n@Composable\nfun AudioMessage(message: MediaStore.Audio) {\n TODO(\"Not yet implemented.\")\n}\n\n@Composable\nfun TextMessage(message: Text) {\n TODO(\"Not yet implemented.\")\n}\n\ndata class SampleMessage(val text: String, val content: Any)https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/lists/ListWithMultipleItemTypes.kt#L25-L51\n```\n\n\u003cbr /\u003e\n\n### Key points about the code\n\n- Specifies the content type for each item by setting [`contentType`](/reference/kotlin/androidx/compose/foundation/lazy/package-summary#extension-functions-summary) in [`items()`](/reference/kotlin/androidx/compose/foundation/lazy/LazyListScope#items(kotlin.Int,kotlin.Function1,kotlin.Function1,kotlin.Function2)).\n- Maps each content type to a corresponding composable. For example, `Audio` is a `contentType` that is defined elsewhere and is mapped to an `AudioMessage` composable.\n- Compose reuses the rendered composables for each item of a given content type.\n\nResults\n-------\n\n**Figure 1.** Code output showing audio and text messages.\n\nCollections that contain this guide\n-----------------------------------\n\nThis guide is part of these curated Quick Guide collections that cover\nbroader Android development goals: \n\n### Display a list or grid\n\nLists and grids allow your app to display collections in a visually pleasing form that's easy for users to consume. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-a-list-or-grid) \n\n### Display interactive components\n\nLearn how composable functions can enable you to easily create beautiful UI components based on the Material Design design system. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/display-interactive-components) \n\n### Compose basics (video collection)\n\nThis series of videos introduces various Compose APIs, quickly showing you what's available and how to use them. \n[Quick guide collection](/develop/ui/compose/quick-guides/collections/compose-basics) \n\nHave questions or feedback\n--------------------------\n\nGo to our frequently asked questions page and learn about quick guides or reach out and let us know your thoughts. \n[Go to FAQ](/quick-guides/faq) [Leave feedback](https://issuetracker.google.com/issues/new?component=1573691&template=1993320)"]]