5

Appbar used to have an issue when flinging. It was not scrolling smoothly.

Please refer to these:

But it has been fixed in support library version 26.

compile 'com.android.support:design:26.0.0'

However, appbar is now bouncing back even if fling is not hard.

enter image description here

How do I remove this behavior?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vida
  • 4,459
  • 1
  • 22
  • 27

1 Answers1

6

This is only happening when AppBar is scrolled/flung while the NestedScrollView(or RecyclerView) has not yet finish flinging.

Solution: Extend AppBar's default Behavior and block the call for AppBar.Behavior's onNestedPreScroll() and onNestedScroll() when AppBar is touched while NestedScroll hasn't stopped yet.

 @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);
    }
}

then use it on the layout:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    ...
    app:layout_behavior="com.mypackage.NoBounceBehavior"/>

Reference for full code of the custom behavior can be found here: https://gist.github.com/ampatron/9d56ea401094f67196f407f82f14551a

vida
  • 4,459
  • 1
  • 22
  • 27
  • 3
    Just an extra note to any readers, remember to add the two class constructors as well in your NoBounce class or it will fail to load the new behavior. – Shadow May 31 '18 at 00:08