Last active
January 25, 2019 05:52
-
-
Save mzgreen/7b77aa4ec95f81c3bed0 to your computer and use it in GitHub Desktop.
HideOnScrollExample - custom fab behavior
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
public class ScrollingFABBehavior extends CoordinatorLayout.Behavior<FloatingActionButton> { | |
private int toolbarHeight; | |
public ScrollingFABBehavior(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
this.toolbarHeight = Utils.getToolbarHeight(context); | |
} | |
@Override | |
public boolean layoutDependsOn(CoordinatorLayout parent, FloatingActionButton fab, View dependency) { | |
return dependency instanceof AppBarLayout; | |
} | |
@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 = (float)dependency.getY()/(float)toolbarHeight; | |
fab.setTranslationY(-distanceToScroll * ratio); | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment