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.
Thành phần kết hợp PredictiveBackHandler trong Jetpack Compose cho phép bạn chặn cử chỉ quay lại và truy cập vào tiến trình của cử chỉ đó. Bạn có thể phản hồi cử chỉ quay lại của người dùng theo thời gian thực, tạo ảnh động hoặc hành vi tuỳ chỉnh dựa trên khoảng cách mà người dùng vuốt.
Để sử dụng PredictiveBackHandler, hãy đảm bảo bạn đang dùng androidx.activity:activity:1.6.0 trở lên.
PredictiveBackHandler cung cấp một Flow<BackEventCompat> phát ra các sự kiện biểu thị tiến trình của thao tác quay lại. Mỗi sự kiện chứa thông tin như:
progress: Một giá trị số thực từ 0 đến 1 cho biết tiến trình của cử chỉ quay lại (0 = cử chỉ bắt đầu, 1 = cử chỉ hoàn tất).
touchX và touchY: Toạ độ X và Y của sự kiện chạm.
Đoạn mã sau đây cho thấy cách sử dụng cơ bản của PredictiveBackHandler:
PredictiveBackHandler(true){progress:Flow<BackEventCompat>->
// code for gesture back startedtry{progress.collect{backEvent->
// code for progressboxScale=1F-(1F*backEvent.progress)}// code for completionboxScale=0F}catch(e:CancellationException){// code for cancellationboxScale=1Fthrowe}}
Ví dụ này minh hoạ cách triển khai một ảnh động tuỳ chỉnh trong ứng dụng bằng cách sử dụng PredictiveBackHandler để tạo một hoạt động tương tác mượt mà với một ngăn điều hướng để phản hồi các cử chỉ quay lại trong JetLagged:
Hình 5. Ngăn điều hướng có hỗ trợ tính năng xem trước thao tác quay lại.
Trong ví dụ này, PredictiveBackHandler được dùng để:
Theo dõi tiến trình của cử chỉ quay lại.
Cập nhật translationX của ngăn dựa trên tiến trình cử chỉ.
Sử dụng velocityTracker để mở hoặc đóng ngăn kéo một cách mượt mà dựa trên vận tốc cử chỉ khi cử chỉ hoàn tất hoặc bị huỷ.
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-08-27 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-08-27 UTC."],[],[],null,["# Access progress manually\n\nThe [`PredictiveBackHandler`](https://developer.android.com/reference/kotlin/androidx/activity/compose/PredictiveBackHandler) composable in Jetpack Compose lets you\nintercept the back gesture and access its progress. You can react to the user's\nback gesture in real-time, creating custom animations or behaviors based on how\nfar the user swipes.\n\nTo use the `PredictiveBackHandler`, ensure you are using\n`androidx.activity:activity:1.6.0` or higher.\n\n`PredictiveBackHandler` provides a `Flow\u003cBackEventCompat\u003e` that emits events\nrepresenting the progress of the back gesture. Each event contains information\nsuch as:\n\n- `progress`: A float value between 0 and 1 indicating the progress of the back gesture (0 = gesture started, 1 = gesture completed).\n- `touchX` and `touchY`: The X and Y coordinates of the touch event.\n\nThe following snippet shows basic usage of `PredictiveBackHandler`:\n\n\n```kotlin\nPredictiveBackHandler(true) { progress: Flow\u003cBackEventCompat\u003e -\u003e\n // code for gesture back started\n try {\n progress.collect { backEvent -\u003e\n // code for progress\n boxScale = 1F - (1F * backEvent.progress)\n }\n // code for completion\n boxScale = 0F\n } catch (e: CancellationException) {\n // code for cancellation\n boxScale = 1F\n throw e\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/predictiveback/PredictiveBackSnippets.kt#L122-L136\n```\n\n\u003cbr /\u003e\n\nExample: Integrate with a navigation drawer\n-------------------------------------------\n\nThis example demonstrates how to implement a custom in-app animation using `PredictiveBackHandler` to create a smooth interaction with a navigation\ndrawer in response to back gestures in [JetLagged](https://github.com/android/compose-samples/blob/main/JetLagged/app/src/main/java/com/example/jetlagged/JetLaggedDrawer.kt):\n**Figure 5.** Navigation drawer with predictive back support.\n\nIn this example, `PredictiveBackHandler` is used to:\n\n- Track the progress of the back gesture.\n- Update the `translationX` of the drawer based on the gesture progress.\n- Use a `velocityTracker` to smoothly open or close the drawer based on the gesture velocity when the gesture is completed or canceled."]]