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.
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.
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.
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.
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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With