Skip to content

Instantly share code, notes, and snippets.

@lodlock
Last active August 19, 2019 10:59
Show Gist options
  • Save lodlock/e3cd12130bad70a098db to your computer and use it in GitHub Desktop.
Save lodlock/e3cd12130bad70a098db to your computer and use it in GitHub Desktop.
Quick behavior that allows for https://github.com/Clans/FloatingActionButton to be compatible with CoordinatorLayout. Add app:layout_behavior="wherever.you.want.to.keep.your.behaviors.FloatingActionMenuBehavior" (of course modify it to match your code) to your FloatingActionMenu that resides within a CoordinatorLayout.
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.util.AttributeSet;
import android.view.View;
import com.github.clans.fab.FloatingActionMenu;
import java.util.List;
public class FloatingActionMenuBehavior extends CoordinatorLayout.Behavior {
private float mTranslationY;
public FloatingActionMenuBehavior(Context context, AttributeSet attrs) {
super();
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
if (child instanceof FloatingActionMenu && dependency instanceof Snackbar.SnackbarLayout) {
this.updateTranslation(parent, child, dependency);
}
return false;
}
private void updateTranslation(CoordinatorLayout parent, View child, View dependency) {
float translationY = this.getTranslationY(parent, child);
if (translationY != this.mTranslationY) {
ViewCompat.animate(child)
.cancel();
if (Math.abs(translationY - this.mTranslationY) == (float) dependency.getHeight()) {
ViewCompat.animate(child)
.translationY(translationY)
.setListener((ViewPropertyAnimatorListener) null);
} else {
ViewCompat.setTranslationY(child, translationY);
}
this.mTranslationY = translationY;
}
}
private float getTranslationY(CoordinatorLayout parent, View child) {
float minOffset = 0.0F;
List dependencies = parent.getDependencies(child);
int i = 0;
for (int z = dependencies.size(); i < z; ++i) {
View view = (View) dependencies.get(i);
if (view instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(child, view)) {
minOffset = Math.min(minOffset, ViewCompat.getTranslationY(view) - (float) view.getHeight());
}
}
return minOffset;
}
}
@leonardonogueira
Copy link

There's a problem when i try to dismiss a Snackbar. The FloatingActionMenu doesn't go back to the bottom. Can you please verify that?

@leonardonogueira
Copy link

I could make it working by adding the following lines to the FloatingActionMenuBehavior class:

 @Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
    if (child instanceof FloatingActionMenu && dependency instanceof Snackbar.SnackbarLayout) {
        this.updateTranslation(parent, child, dependency);
    }
}

@RanyAlbegWein
Copy link

This is very helpful, thank you very much!

@espinoco
Copy link

Thanks a lot

@mukutbhattacharjee
Copy link

+1 for that onDependentViewRemoved(). Thanks a lot

@vaavaa
Copy link

vaavaa commented Mar 28, 2016

+100 to karma. Thank you!

@kyze8439690
Copy link

Modified to support FloatingActionButton

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
    if ((child instanceof FloatingActionMenu || child instanceof FloatingActionButton)
            && dependency instanceof Snackbar.SnackbarLayout) {
        this.updateTranslation(parent, child, dependency);
    }

    return false;
}

@Override
public void onDependentViewRemoved(CoordinatorLayout parent, View child, View dependency) {
    if ((child instanceof FloatingActionMenu || child instanceof FloatingActionButton)
            && dependency instanceof Snackbar.SnackbarLayout) {
        this.updateTranslation(parent, child, dependency);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment