Se debe englobar en un CoordinatorLayout
ejemplo de la jerarquia:
CoordinatorLayout
AppBarLayout
Toolbar
RecyclerView
FloatingActionButton
Para interceptar el scroll
public class FabScrollBehavior extends FloatingActionButton.Behavior {
public FabScrollBehavior(Context context, AttributeSet attributeSet) {
super();
}
@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.GONE) {
child.show();
}
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
}
y en el FloatingActionButton
donde queremos interceptar que se haga scroll en el recycleview
...
app:layout_behavior="tu.pcakage.android.FabScrollBehavior"
...
Tutorial Hide the FloatingActionButton when scrolling a RecyclerView