با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
PredictiveBackHandler قابل تنظیم در Jetpack Compose به شما امکان می دهد ژست عقب را متوقف کنید و به پیشرفت آن دسترسی داشته باشید. میتوانید به ژست برگشتی کاربر در زمان واقعی واکنش نشان دهید و انیمیشنها یا رفتارهای سفارشی را بر اساس میزان سوایپ کاربر ایجاد کنید.
برای استفاده از PredictiveBackHandler ، مطمئن شوید که از androidx.activity:activity:1.6.0 یا بالاتر استفاده می کنید.
PredictiveBackHandler یک Flow<BackEventCompat> ارائه میکند که رویدادهایی را منتشر میکند که نشاندهنده پیشرفت حرکت برگشتی است. هر رویداد حاوی اطلاعاتی مانند:
progress : یک مقدار شناور بین 0 و 1 که نشان دهنده پیشرفت حرکت برگشتی است (0 = اشاره شروع شد، 1 = حرکت کامل شد).
touchX و touchY : مختصات X و Y رویداد لمسی.
قطعه زیر استفاده اساسی از 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}}
این مثال نشان میدهد که چگونه میتوان یک انیمیشن درون برنامهای سفارشی را با استفاده از PredictiveBackHandler پیادهسازی کرد تا تعاملی صاف با کشوی پیمایش در پاسخ به حرکات برگشتی در JetLagged ایجاد شود:
شکل 5. کشوی ناوبری با پشتیبان پیش بینی کننده.
در این مثال، PredictiveBackHandler برای موارد زیر استفاده می شود:
پیشرفت حرکت ژست عقب را پیگیری کنید.
translationX کشو را بر اساس پیشرفت حرکت بهروزرسانی کنید.
از velocityTracker برای باز کردن یا بستن هموار کشو بر اساس سرعت حرکت هنگام تکمیل یا لغو حرکت استفاده کنید.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","easyToUnderstand","thumb-up"],["مشکلم را برطرف کرد","solvedMyProblem","thumb-up"],["غیره","otherUp","thumb-up"]],[["اطلاعاتی که نیاز دارم وجود ندارد","missingTheInformationINeed","thumb-down"],["بیشازحد پیچیده/ مراحل بسیار زیاد","tooComplicatedTooManySteps","thumb-down"],["قدیمی","outOfDate","thumb-down"],["مشکل ترجمه","translationIssue","thumb-down"],["مشکل کد / نمونهها","samplesCodeIssue","thumb-down"],["غیره","otherDown","thumb-down"]],["تاریخ آخرین بهروزرسانی 2025-08-27 بهوقت ساعت هماهنگ جهانی."],[],[],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."]]