15

I've been using this FloatingActionButton.Behavior for many months now, it takes care of hiding and showing the FAB of my application. Never had an issue.

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {
public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
    super();
}

@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                                   final View directTargetChild, final View target, final int nestedScrollAxes) {
    // Ensure we react to vertical scrolling
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
            || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}

@Override
public void onNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child,
                           final View target, final int dxConsumed, final int dyConsumed,
                           final int dxUnconsumed, final int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
    if (dyConsumed > 0 && child.getVisibility() == View.VISIBLE) {
        // User scrolled down and the FAB is currently visible -> hide the FAB
        child.hide();
    } else if (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) {
        // User scrolled up and the FAB is currently not visible -> show the FAB
        child.show();
    }
}}

As soon as I update to the support library

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

the FloatingActionButton.Behavior stops working properly. It hides the FAB once and then it's never called again. Not the onStartNestedScroll or the onNestedScroll method.

Does anyone know what's happening here. The rest of my code remains unchanged, I only update this library and it stops working as before.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Roberto
  • 1,793
  • 1
  • 18
  • 19
  • There is a new version compile 'com.android.support:design:25.2.0' and the error still there, so far I'm not updating this library, stuck with 25.0.0 – Roberto Mar 01 '17 at 10:19
  • So far I'm using the solution from @woxingxiao and it's working well with version 25.2.0 – Roberto Apr 05 '17 at 11:13

2 Answers2

43

For 25.1.0, CoordinatorLayout is skipping views set to GONE when looking for behaviors to call in its onNestedScroll method.
The solution is replacing FAB's visibility GONE with INVISIBLE.
Simply, change:

child.hide();  

to:

child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
        @Override
        public void onHidden(FloatingActionButton fab) {
            super.onHidden(fab);
            fab.setVisibility(View.INVISIBLE);
        }
    });
woxingxiao
  • 963
  • 9
  • 13
  • @Lancelot If this solved your problem, you should accept the answer by clicking the checkmark so others know it is an actual solution and not just a potential one. – TylerH Nov 29 '18 at 16:04
6

I have absolutely the same problem, looks like there is a bug in 'support-design package'. The only thing that we can do now, I believe, is rollback to one of previous versions. I decided to use the following:

compile 'com.android.support:design:25.0.1'
daxh
  • 571
  • 7
  • 14
  • I'm using that version too as I couldn't find any other solution at that time. I think there is another way of doing it but means changing the behavior class. – Roberto Jan 23 '17 at 17:38