From 5b9f27aad0bb185261d6e400274a416953755d74 Mon Sep 17 00:00:00 2001 From: conradchen Date: Mon, 18 Apr 2022 11:59:31 -0400 Subject: [PATCH] [SnackBar] Make dismiss direction the same as swipe direction In SwipeDismissBehavior, when dismissing views according to the horizontal velocity instead of position delta, we would still using position delta to decide the dismissing direction, which, unfortunately, makes the view always be dismissed from left to right because of the "less than" logic we are using doesn't really take the equaling case into account. Fixes this by using the velocity direction to dismiss the view instead. Resolves https://github.com/material-components/material-components-android/issues/1868 PiperOrigin-RevId: 442553318 --- .../material/behavior/SwipeDismissBehavior.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/java/com/google/android/material/behavior/SwipeDismissBehavior.java b/lib/java/com/google/android/material/behavior/SwipeDismissBehavior.java index e525014cd4c..9e5c7f7b09f 100644 --- a/lib/java/com/google/android/material/behavior/SwipeDismissBehavior.java +++ b/lib/java/com/google/android/material/behavior/SwipeDismissBehavior.java @@ -263,7 +263,7 @@ public void onViewDragStateChanged(int state) { } @Override - public void onViewReleased(@NonNull View child, float xvel, float yvel) { + public void onViewReleased(@NonNull View child, float xVelocity, float yVelocity) { // Reset the active pointer ID activePointerId = INVALID_POINTER_ID; @@ -271,9 +271,9 @@ public void onViewReleased(@NonNull View child, float xvel, float yvel) { int targetLeft; boolean dismiss = false; - if (shouldDismiss(child, xvel)) { + if (shouldDismiss(child, xVelocity)) { targetLeft = - child.getLeft() < originalCapturedViewLeft + xVelocity < 0f || child.getLeft() < originalCapturedViewLeft ? originalCapturedViewLeft - childWidth : originalCapturedViewLeft + childWidth; dismiss = true; @@ -289,8 +289,8 @@ public void onViewReleased(@NonNull View child, float xvel, float yvel) { } } - private boolean shouldDismiss(@NonNull View child, float xvel) { - if (xvel != 0f) { + private boolean shouldDismiss(@NonNull View child, float xVelocity) { + if (xVelocity != 0f) { final boolean isRtl = ViewCompat.getLayoutDirection(child) == ViewCompat.LAYOUT_DIRECTION_RTL; @@ -300,11 +300,11 @@ private boolean shouldDismiss(@NonNull View child, float xvel) { } else if (swipeDirection == SWIPE_DIRECTION_START_TO_END) { // We only allow start-to-end swiping, so the fling needs to be in the // correct direction - return isRtl ? xvel < 0f : xvel > 0f; + return isRtl ? xVelocity < 0f : xVelocity > 0f; } else if (swipeDirection == SWIPE_DIRECTION_END_TO_START) { // We only allow end-to-start swiping, so the fling needs to be in the // correct direction - return isRtl ? xvel > 0f : xvel < 0f; + return isRtl ? xVelocity > 0f : xVelocity < 0f; } } else { final int distance = child.getLeft() - originalCapturedViewLeft;