Skip to content

Instantly share code, notes, and snippets.

@fnzainal
Forked from lodlock/FloatingActionMenuBehavior
Created December 17, 2016 00:56
Show Gist options
  • Save fnzainal/1429c4e6b1b5f5b5655e78a6e3b18e9c to your computer and use it in GitHub Desktop.
Save fnzainal/1429c4e6b1b5f5b5655e78a6e3b18e9c 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment