Skip to content

Commit 5b9f27a

Browse files
committed
[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 #1868 PiperOrigin-RevId: 442553318
1 parent 788866e commit 5b9f27a

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

lib/java/com/google/android/material/behavior/SwipeDismissBehavior.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,17 +263,17 @@ public void onViewDragStateChanged(int state) {
263263
}
264264

265265
@Override
266-
public void onViewReleased(@NonNull View child, float xvel, float yvel) {
266+
public void onViewReleased(@NonNull View child, float xVelocity, float yVelocity) {
267267
// Reset the active pointer ID
268268
activePointerId = INVALID_POINTER_ID;
269269

270270
final int childWidth = child.getWidth();
271271
int targetLeft;
272272
boolean dismiss = false;
273273

274-
if (shouldDismiss(child, xvel)) {
274+
if (shouldDismiss(child, xVelocity)) {
275275
targetLeft =
276-
child.getLeft() < originalCapturedViewLeft
276+
xVelocity < 0f || child.getLeft() < originalCapturedViewLeft
277277
? originalCapturedViewLeft - childWidth
278278
: originalCapturedViewLeft + childWidth;
279279
dismiss = true;
@@ -289,8 +289,8 @@ public void onViewReleased(@NonNull View child, float xvel, float yvel) {
289289
}
290290
}
291291

292-
private boolean shouldDismiss(@NonNull View child, float xvel) {
293-
if (xvel != 0f) {
292+
private boolean shouldDismiss(@NonNull View child, float xVelocity) {
293+
if (xVelocity != 0f) {
294294
final boolean isRtl =
295295
ViewCompat.getLayoutDirection(child) == ViewCompat.LAYOUT_DIRECTION_RTL;
296296

@@ -300,11 +300,11 @@ private boolean shouldDismiss(@NonNull View child, float xvel) {
300300
} else if (swipeDirection == SWIPE_DIRECTION_START_TO_END) {
301301
// We only allow start-to-end swiping, so the fling needs to be in the
302302
// correct direction
303-
return isRtl ? xvel < 0f : xvel > 0f;
303+
return isRtl ? xVelocity < 0f : xVelocity > 0f;
304304
} else if (swipeDirection == SWIPE_DIRECTION_END_TO_START) {
305305
// We only allow end-to-start swiping, so the fling needs to be in the
306306
// correct direction
307-
return isRtl ? xvel > 0f : xvel < 0f;
307+
return isRtl ? xVelocity > 0f : xVelocity < 0f;
308308
}
309309
} else {
310310
final int distance = child.getLeft() - originalCapturedViewLeft;

0 commit comments

Comments
 (0)