Skip to content

Commit

Permalink
#4
Browse files Browse the repository at this point in the history
Fixed closing/opening bottom sheet when ViewPager is scrolled.
  • Loading branch information
kozmi55 authored and laenger committed Nov 5, 2019
1 parent fa16d0c commit 014590b
Showing 1 changed file with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public abstract static class BottomSheetCallback {

private static final float HIDE_FRICTION = 0.1f;

private float mMinimumVelocity;

private float mMaximumVelocity;

private int mPeekHeight;
Expand All @@ -146,8 +148,6 @@ public abstract static class BottomSheetCallback {

private boolean mIgnoreEvents;

private int mLastNestedScrollDy;

private boolean mNestedScrolled;

int mParentHeight;
Expand Down Expand Up @@ -195,6 +195,7 @@ public ViewPagerBottomSheetBehavior(Context context, AttributeSet attrs) {
a.recycle();
ViewConfiguration configuration = ViewConfiguration.get(context);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
}

@Override
Expand Down Expand Up @@ -338,7 +339,6 @@ public boolean onTouchEvent(CoordinatorLayout parent, V child, MotionEvent event
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, V child,
View directTargetChild, View target, int nestedScrollAxes) {
mLastNestedScrollDy = 0;
mNestedScrolled = false;
return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
Expand Down Expand Up @@ -376,7 +376,6 @@ public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, V child, View
}
}
dispatchOnSlide(child.getTop());
mLastNestedScrollDy = dy;
mNestedScrolled = true;
}

Expand All @@ -390,15 +389,24 @@ public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, Vie
|| !mNestedScrolled) {
return;
}

mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
float xVel = mVelocityTracker.getXVelocity(mActivePointerId);
float yVel = mVelocityTracker.getYVelocity(mActivePointerId);

int top;
int targetState;
if (mLastNestedScrollDy > 0) {
if (yVel < 0 && Math.abs(yVel) > mMinimumVelocity && Math.abs(yVel) > Math.abs(xVel)) {
top = mMinOffset;
targetState = STATE_EXPANDED;
} else if (mHideable && shouldHide(child, getYVelocity())) {
} else if (mHideable && shouldHide(child, yVel)) {
top = mParentHeight;
targetState = STATE_HIDDEN;
} else if (mLastNestedScrollDy == 0) {
} else if (yVel > 0 && Math.abs(yVel) > mMinimumVelocity && Math.abs(yVel) > Math.abs(xVel)) {
top = mMaxOffset;
targetState = STATE_COLLAPSED;
} else {
// not scrolling much, i.e. stationary
int currentTop = child.getTop();
if (Math.abs(currentTop - mMinOffset) < Math.abs(currentTop - mMaxOffset)) {
top = mMinOffset;
Expand All @@ -407,10 +415,8 @@ public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, V child, Vie
top = mMaxOffset;
targetState = STATE_COLLAPSED;
}
} else {
top = mMaxOffset;
targetState = STATE_COLLAPSED;
}

if (mViewDragHelper.smoothSlideViewTo(child, child.getLeft(), top)) {
setStateInternal(STATE_SETTLING);
ViewCompat.postOnAnimation(child, new SettleRunnable(child, targetState));
Expand Down Expand Up @@ -692,13 +698,17 @@ public void onViewDragStateChanged(int state) {
public void onViewReleased(View releasedChild, float xvel, float yvel) {
int top;
@State int targetState;
if (yvel < 0) { // Moving up
if (yvel < 0 && Math.abs(yvel) > mMinimumVelocity && Math.abs(yvel) > Math.abs(xvel)) {
top = mMinOffset;
targetState = STATE_EXPANDED;
} else if (mHideable && shouldHide(releasedChild, yvel)) {
top = mParentHeight;
targetState = STATE_HIDDEN;
} else if (yvel == 0.f) {
} else if (yvel > 0 && Math.abs(yvel) > mMinimumVelocity && Math.abs(yvel) > Math.abs(xvel)) {
top = mMaxOffset;
targetState = STATE_COLLAPSED;
} else {
// not scrolling much, i.e. stationary
int currentTop = releasedChild.getTop();
if (Math.abs(currentTop - mMinOffset) < Math.abs(currentTop - mMaxOffset)) {
top = mMinOffset;
Expand All @@ -707,10 +717,8 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) {
top = mMaxOffset;
targetState = STATE_COLLAPSED;
}
} else {
top = mMaxOffset;
targetState = STATE_COLLAPSED;
}

if (mViewDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top)) {
setStateInternal(STATE_SETTLING);
ViewCompat.postOnAnimation(releasedChild,
Expand Down

0 comments on commit 014590b

Please sign in to comment.