content: package fuzion24.device.vulnerability.test;
  
  import android.content.Context;
  import android.support.design.widget.AppBarLayout;
  import android.support.design.widget.CoordinatorLayout;
  import android.support.design.widget.FloatingActionButton;
  import android.support.design.widget.Snackbar;
  import android.util.AttributeSet;
  import android.view.View;
  
  import fuzion24.device.vulnerability.util.GenericUtils;
  
  public class ScrollingFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> {
  
      private int toolbarHeight;
  
      public ScrollingFABBehavior(Context context, AttributeSet attrs) {
          super(context, attrs);
  
          this.toolbarHeight = GenericUtils.getActionBarHeight(context);
      }
  
      @Override
      public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
          return dependency instanceof AppBarLayout || dependency instanceof Snackbar.SnackbarLayout;
      }
  
      @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 = dependency.getY() / (float) toolbarHeight;
  
              fab.setTranslationY(-distanceToScroll * ratio);
  
              return true;
          }
  
          if (dependency instanceof Snackbar.SnackbarLayout) {
              float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
  
              fab.setTranslationY(translationY);
  
              return true;
          }
  
          return false;
      }
  
  }
  