0

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?

vankiz
  • 96
  • 1
  • 6
  • You need to implement both behaviors in `CustomBehavior`. This post should give you the implemention details to replicate the default behavior: https://lab.getbase.com/introduction-to-coordinator-layout-on-android/. – siger Aug 05 '15 at 05:40

1 Answers1

0

you'd need to keep a local instance of behavior and hold on to the default behavior. try to set the default behavior back when snackbar is dismissed and then your custom behavior when it's shown. something like:

mSnackbar.setCallback(new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);
                ((CoordinatorLayout.LayoutParams)mSnackbar.getView().getLayoutParams()).setBehavior(/*default behavior*/);
            }

            @Override
            public void onShown(Snackbar snackbar) {
                super.onShown(snackbar);
                ((CoordinatorLayout.LayoutParams)mSnackbar.getView().getLayoutParams()).setBehavior(/*your custom behavior*/);

            }
        });

good luck!

ANemati
  • 4,478
  • 3
  • 17
  • 13