Skip to content

Instantly share code, notes, and snippets.

@ampatron
Last active September 11, 2020 09:51
Show Gist options
  • Save ampatron/9d56ea401094f67196f407f82f14551a to your computer and use it in GitHub Desktop.
Save ampatron/9d56ea401094f67196f407f82f14551a to your computer and use it in GitHub Desktop.
Custom behavior to prevent v26 AppBar support from bouncing when scrolling
public class NoBounceBehavior extends AppBarLayout.Behavior {
private static final int TYPE_FLING = 1;
...
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
if (isFlinging) {
shouldBlockNestedScroll = true;
}
return super.onInterceptTouchEvent(parent, child, ev);
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
if (type == TYPE_FLING) {
isFlinging = true;
}
if (!shouldBlockNestedScroll) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
}
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
if (!shouldBlockNestedScroll) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
}
}
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target, int type) {
super.onStopNestedScroll(coordinatorLayout, abl, target, type);
isFlinging = false;
shouldBlockNestedScroll = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment