Created
June 29, 2015 00:21
-
-
Save wakim/62ec793815b62203229c to your computer and use it in GitHub Desktop.
Generic Snack Push Behavior (Refactored from FAB)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package br.com.app4y.behavior; | |
| 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.animation.FastOutSlowInInterpolator; | |
| import android.util.AttributeSet; | |
| import android.view.View; | |
| import android.view.animation.Interpolator; | |
| import java.util.List; | |
| public class SnackPushBehavior extends CoordinatorLayout.Behavior<View> { | |
| static final Interpolator FAST_OUT_SLOW_IN_INTERPOLATOR = new FastOutSlowInInterpolator(); | |
| private float mTranslationY; | |
| public SnackPushBehavior(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| } | |
| @Override | |
| public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { | |
| if(dependency instanceof Snackbar.SnackbarLayout) { | |
| updateViewTranslationForSnackbar(parent, child, dependency); | |
| } | |
| return false; | |
| } | |
| private void updateViewTranslationForSnackbar(CoordinatorLayout parent, View view, View snackbar) { | |
| float translationY = this.getFabTranslationYForSnackbar(parent, view); | |
| if(translationY != this.mTranslationY) { | |
| ViewCompat.animate(view).cancel(); | |
| if(Math.abs(translationY - this.mTranslationY) == (float) snackbar.getHeight()) { | |
| ViewCompat.animate(view).translationY(translationY).setInterpolator(FAST_OUT_SLOW_IN_INTERPOLATOR).setListener(null); | |
| } else { | |
| ViewCompat.setTranslationY(view, translationY); | |
| } | |
| this.mTranslationY = translationY; | |
| } | |
| } | |
| private float getFabTranslationYForSnackbar(CoordinatorLayout parent, View view) { | |
| float minOffset = 0.0F; | |
| List dependencies = parent.getDependencies(view); | |
| int i = 0; | |
| for(int z = dependencies.size(); i < z; ++i) { | |
| View dependency = (View)dependencies.get(i); | |
| if(dependency instanceof Snackbar.SnackbarLayout && parent.doViewsOverlap(view, dependency)) { | |
| minOffset = Math.min(minOffset, ViewCompat.getTranslationY(dependency) - (float) dependency.getHeight()); | |
| } | |
| } | |
| return minOffset; | |
| } | |
| @Override | |
| public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { | |
| return dependency instanceof Snackbar.SnackbarLayout; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment