@Override publicbooleanonStartNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View directTargetChild, final View target, finalint nestedScrollAxes) { // Ensure we react to vertical scrolling return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL || super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes); }
@Override publicvoidonNestedScroll(final CoordinatorLayout coordinatorLayout, final FloatingActionButton child, final View target, finalint dxConsumed, finalint dyConsumed, finalint dxUnconsumed, finalint dyUnconsumed) { super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed); if (dyConsumed > 0 && !this.mIsAnimatingOut && child.getVisibility() == View.VISIBLE) { // User scrolled down and the FAB is currently visible -> hide the FAB animateOut(child); } elseif (dyConsumed < 0 && child.getVisibility() != View.VISIBLE) { // User scrolled up and the FAB is currently not visible -> show the FAB animateIn(child); } }
// Same animation that FloatingActionButton.Behavior uses to hide the FAB when the AppBarLayout exits privatevoidanimateOut(final FloatingActionButton button) { if (Build.VERSION.SDK_INT >= 14) { ViewCompat.animate(button).translationY(button.getHeight() + getMarginBottom(button)).setInterpolator(INTERPOLATOR).withLayer() .setListener(newViewPropertyAnimatorListener() { publicvoidonAnimationStart(View view) { ScrollAwareFABBehavior.this.mIsAnimatingOut = true; }
// Same animation that FloatingActionButton.Behavior uses to show the FAB when the AppBarLayout enters privatevoidanimateIn(FloatingActionButton button) { button.setVisibility(View.VISIBLE); if (Build.VERSION.SDK_INT >= 14) { ViewCompat.animate(button).translationY(0) .setInterpolator(INTERPOLATOR).withLayer().setListener(null) .start(); } else { button.show(); } }
privateintgetMarginBottom(View v) { intmarginBottom=0; final ViewGroup.LayoutParamslayoutParams= v.getLayoutParams(); if (layoutParams instanceof ViewGroup.MarginLayoutParams) { marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin; } return marginBottom; }