Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 272
xdbas Avatar asked Feb 06 '17 13:02

xdbas


People also ask

What does a floating action button do?

A floating action button (FAB) is a circular button that triggers the primary action in your app's UI. This page shows you how to add the FAB to your layout, customize some of its appearance, and respond to button taps.

How do you hide and show floating action button in flutter?

Build the widget Inside the Listview, or Listview. builder we should have to set the controller object. And also for the Floating action button, we are wrapping the button with a visibility widget and we should have to set the visible attribute to _show.

What is extended floating action button?

A floating action button (FAB) performs the primary, or most common, action on a screen. It appears in front of all screen content, typically as a circular shape with an icon in its center. Extended Floating Action Button is the newly introduced class with Material Components library in Android.

How do I hide the floating action button?

Example# To show and hide a FloatingActionButton with the default animation, just call the methods show() and hide() . It's good practice to keep a FloatingActionButton in the Activity layout instead of putting it in a Fragment, this allows the default animations to work when showing and hiding.


1 Answers

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);
        }
    }); 
like image 65
xdbas Avatar answered Oct 11 '22 23:10

xdbas