I have a FAB (Floating Action Button)
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="@dimen/fab_margin"
app:layout_anchorGravity="bottom|right|end"
android:src="@drawable/img"
app:borderWidth="0dp"
/>
This FAB exists on top of a RecycleView that I scroll through. At certain stage of scrolling I show SnackBar and everything works as advertised (specifically, the SnackBar shifts the FAB to avoid blockage.
Now I want to implement a custom behavior, the one that on scroll down I want to hide FAB.
So I have created a custom behavior:
public class CustomBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
private int toolbarHeight;
public CustomBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
this.toolbarHeight = Utils.getToolbarHeight(context);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
return dependency instanceof AppBarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
if (dependency instanceof AppBarLayout) {
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
int fabBottomMargin = lp.bottomMargin;
int distanceToScroll = fab.getHeight() + fabBottomMargin;
float ratio = (float)dependency.getY()/(float)toolbarHeight;
fab.setTranslationY(-distanceToScroll * ratio);
}
return true;
}
}
This pretty much hides my FAB when I scroll down (it hides together with AppBarLayout So I add this to my FAB xml declaration
app:layout_behavior="com.example.CustomBehavior"
When I do this, the FAB properly hides, but the SnackBar overlaps it when it shows. Meaning the default behavior is gone...
Is it possible to have both?