From 2b466d4790cf09be424b024708125ff958cd9051 Mon Sep 17 00:00:00 2001 From: Ayman Ahmed Date: Sat, 3 Jun 2017 08:55:53 +0300 Subject: [PATCH] Add default behavior with Snakebars Its main function is to move FloatingActionMenu views so that any displayed Snackbars do not cover them. No need to add layout_behavior in xml as it added by default by annotating the class. --- .../github/clans/fab/FloatingActionMenu.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java b/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java index 0b80e4c..9c0d6c2 100755 --- a/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java +++ b/library/src/main/java/com/github/clans/fab/FloatingActionMenu.java @@ -10,6 +10,9 @@ import android.graphics.Typeface; import android.graphics.drawable.Drawable; import android.os.Handler; +import android.support.design.widget.CoordinatorLayout; +import android.support.design.widget.Snackbar; +import android.support.v4.view.ViewCompat; import android.text.TextUtils; import android.util.AttributeSet; import android.util.TypedValue; @@ -23,11 +26,13 @@ import android.view.animation.AnticipateInterpolator; import android.view.animation.Interpolator; import android.view.animation.OvershootInterpolator; +import android.util.AttributeSet; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; +@CoordinatorLayout.DefaultBehavior(FloatingActionMenu.Behavior.class) public class FloatingActionMenu extends ViewGroup { private static final int ANIMATION_DURATION = 300; @@ -1013,4 +1018,37 @@ public void setOnMenuButtonClickListener(OnClickListener clickListener) { public void setOnMenuButtonLongClickListener(OnLongClickListener longClickListener) { mMenuButton.setOnLongClickListener(longClickListener); } + + /** + * Behavior designed for use with {@link FloatingActionMenu} instances. Its main function + * is to move {@link FloatingActionMenu} views so that any displayed {@link Snackbar}s do + * not cover them. + */ + public static class Behavior extends CoordinatorLayout.Behavior { + public Behavior(){ + super(); + } + + public Behavior(Context context, AttributeSet attrs){ + super(context, attrs); + } + + @Override + public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionMenu child, View dependency) { + return dependency instanceof Snackbar.SnackbarLayout; + } + + @Override + public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionMenu child, View dependency) { + float translationY = Math.min(0, ViewCompat.getTranslationY(dependency) - dependency.getHeight()); + ViewCompat.setTranslationY(child, translationY); + return true; + } + + @Override + public void onDependentViewRemoved(CoordinatorLayout parent, FloatingActionMenu child, View dependency) { + ViewCompat.animate(child).translationY(0).start(); + + } + } }