Roberto
Roberto

Reputation: 1813

Android design library 25.1.0 causes FloatingActionButton.Behavior to stop working

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.

Upvotes: 15

Views: 2542

Answers (2)

woxingxiao
woxingxiao

Reputation: 963

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

Upvotes: 43

daxh
daxh

Reputation: 571

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'

Upvotes: 6

Related Questions