Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tạo danh sách phân trang để người dùng có thể cuộn để truy cập vào nội dung quá lớn để vừa với một màn hình. Danh sách phân trang theo chiều ngang có thể giúp người dùng di chuyển qua nội dung như hình ảnh, trình chiếu hoặc băng chuyền sản phẩm. Danh sách phân trang theo chiều dọc rất hữu ích cho các ứng dụng có nhiều nội dung mà người dùng có thể cần phải cuộn qua một số lượng lớn mục, chẳng hạn như bài viết.
Khả năng tương thích của phiên bản
Phương thức triển khai này yêu cầu bạn phải đặt minSDK của dự án thành API cấp 21 trở lên.
Phần phụ thuộc
Tạo danh sách phân trang
Bạn có thể định cấu hình danh sách phân trang theo chiều ngang hoặc chiều dọc, tuỳ thuộc vào hướng cần thiết cho ứng dụng của bạn. Mã sau đây tạo một danh sách phân trang theo chiều ngang hiển thị 10 mục:
Các điểm chính về mã
Thành phần kết hợp HorizontalPager cung cấp danh sách các mục có thể cuộn theo chiều ngang.
Để tạo danh sách phân trang theo chiều dọc, hãy sử dụng thành phần kết hợp VerticalPager.
Mỗi trang trong danh sách chứa một đối tượng Text hiển thị chuỗi "Trang" và số chỉ mục trang.
Một thực thể của rememberPagerState() duy trì trạng thái của trang khi người dùng rời khỏi và hiển thị cùng một trang khi người dùng quay lại trang đó.
Kết quả
Hình 1. Bản minh hoạ HorizontalPager.
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,["# Display a paging list\n\n\u003cbr /\u003e\n\nCreate a paging list so that users can scroll to access content too large to fit\non a single screen. Horizontal paging lists can help users navigate through\ncontent such as images, slideshows, or product carousels. Vertical paging lists\nare useful for content-heavy apps where users may need to scroll through a large\nnumber of items, such as articles.\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\nCreate a paging list\n--------------------\n\nYou can configure a horizontal or vertical paging list, depending on the\norientation required for your app. The following code creates a horizontal\npaging list displaying 10 items:\n\n val pagerState = rememberPagerState(pageCount = {\n 10\n })\n HorizontalPager(state = pagerState) { page -\u003e\n Text(\n text = \"Page: $page\",\n modifier = Modifier.fillMaxWidth()\n )\n }\n\n### Key points about the code\n\n- The [`HorizontalPager`](/reference/kotlin/androidx/compose/foundation/pager/package-summary#HorizontalPager(androidx.compose.foundation.pager.PagerState,androidx.compose.ui.Modifier,androidx.compose.foundation.layout.PaddingValues,androidx.compose.foundation.pager.PageSize,kotlin.Int,androidx.compose.ui.unit.Dp,androidx.compose.ui.Alignment.Vertical,androidx.compose.foundation.gestures.TargetedFlingBehavior,kotlin.Boolean,kotlin.Boolean,kotlin.Function1,androidx.compose.ui.input.nestedscroll.NestedScrollConnection,androidx.compose.foundation.gestures.snapping.SnapPosition,kotlin.Function2)) composable provides a horizontally scrollable list of items.\n - To create a vertical paging list, use the [`VerticalPager`](/reference/kotlin/androidx/compose/foundation/pager/package-summary#VerticalPager(androidx.compose.foundation.pager.PagerState,androidx.compose.ui.Modifier,androidx.compose.foundation.layout.PaddingValues,androidx.compose.foundation.pager.PageSize,kotlin.Int,androidx.compose.ui.unit.Dp,androidx.compose.ui.Alignment.Horizontal,androidx.compose.foundation.gestures.TargetedFlingBehavior,kotlin.Boolean,kotlin.Boolean,kotlin.Function1,androidx.compose.ui.input.nestedscroll.NestedScrollConnection,androidx.compose.foundation.gestures.snapping.SnapPosition,kotlin.Function2)) composable instead.\n- Each page in the list contains a [`Text`](/reference/kotlin/androidx/compose/material/package-summary#Text(androidx.compose.ui.text.AnnotatedString,androidx.compose.ui.Modifier,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.font.FontStyle,androidx.compose.ui.text.font.FontWeight,androidx.compose.ui.text.font.FontFamily,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.style.TextDecoration,androidx.compose.ui.text.style.TextAlign,androidx.compose.ui.unit.TextUnit,androidx.compose.ui.text.style.TextOverflow,kotlin.Boolean,kotlin.Int,kotlin.Int,kotlin.collections.Map,kotlin.Function1,androidx.compose.ui.text.TextStyle)) object that displays the string \"Page\" and the page index number.\n- An instance of [`rememberPagerState()`](/reference/kotlin/androidx/compose/foundation/pager/package-summary#rememberPagerState(kotlin.Int,kotlin.Float)) persists a page's state when the user navigates away, and displays the same page when the user returns to it.\n\nResults\n-------\n\n**Figure 1.** Demo of `HorizontalPager`.\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)"]]