Skip to content

Commit

Permalink
[SnackBar] Make dismiss direction the same as swipe direction
Browse files Browse the repository at this point in the history
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
  • Loading branch information
drchen committed Apr 18, 2022
1 parent 788866e commit 5b9f27a
Showing 1 changed file with 7 additions and 7 deletions.
Expand Up @@ -263,17 +263,17 @@ 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;

final int childWidth = child.getWidth();
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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 5b9f27a

Please sign in to comment.