xdbas
xdbas

Reputation: 723

Floating action button (fab) behavior stops onNestedScroll after hide

I implemented a simple hide/show behavior for the floating action button.

The onNestedScroll event gets called until hide() or setVisiblity(View.GONE) is called on the floating actionbutton then it stops reacting to scroll events. It seems when the fab's visibility gets changed to GONE it stops reacting to scroll events.

public class ScrollAwareFABBehavior extends FloatingActionButton.Behavior {

public ScrollAwareFABBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}


@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout,
                                   final FloatingActionButton child,
                                   final View directTargetChild, final View target,
                                   final int nestedScrollAxes) {

     return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL
            || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child,
                           View target, int dxConsumed, int dyConsumed, int dxUnconsumed,
                           int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed,
            dyUnconsumed);

    if (dyConsumed > 0
            && child.getVisibility() == View.VISIBLE) {
        child.hide();
    } else if (dyConsumed < 0
            && child.getVisibility() != View.VISIBLE) {
                child.show();
            }
        }
    }
}

additional information: When I use manually set the visiblity to invisible it works. But then I am missing the animation.

Upvotes: 10

Views: 1830

Answers (1)

xdbas
xdbas

Reputation: 723

Seems like it's possible to change the behavior of the hide anmation as described here: https://stackoverflow.com/a/41386278/1038102

child.hide(new FloatingActionButton.OnVisibilityChangedListener() {
        @Override
        public void onHidden(FloatingActionButton fab) {
            super.onHidden(fab);
            fab.setVisibility(View.INVISIBLE);
        }
    }); 

Upvotes: 12

Related Questions