SHARE
TWEET

ScrollingFABBehavior

a guest Jul 23rd, 2015 123 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by tilemachos on 7/12/15.
  3.  */
  4. public class ScrollingFABBehavior extends FloatingActionButton.Behavior {
  5.     private final int toolbarHeight;
  6.  
  7.     public ScrollingFABBehavior(Context context, AttributeSet attrs) {
  8.         super();
  9.         this.toolbarHeight = Utils.getToolbarHeight(context);
  10.     }
  11.  
  12.     @Override
  13.     public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
  14.         return super.layoutDependsOn(parent, fab, dependency) || (dependency instanceof AppBarLayout) ;
  15.     }
  16.  
  17.     @Override
  18.     public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
  19.         if (dependency instanceof AppBarLayout) {
  20.             try{
  21.                 CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
  22.                 int fabBottomMargin = lp.bottomMargin;
  23.                 int distanceToScroll = fab.getHeight() + fabBottomMargin;
  24.                 float ratio = dependency.getY()/(float)toolbarHeight;
  25.                 fab.setTranslationY(-distanceToScroll * ratio);
  26.             } catch (ArithmeticException ignored){
  27.  
  28.             }
  29.         }
  30.         return true;
  31.     }
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top