I would like to achieve behaviour similar to this example
but without toolbar move and for custom view not for a FAB. So, firstly i would like to see layou
First you need to extend default FAB behavior, in order to keep FAB behavior when Snackbar
is shown. Otherwise you will see it not translating upwards when Snackbar
pops up.
React on vertical scrolling only:
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent,
View child, View target, View target,int scrollAxes) {
return (scrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
}
Once you have vertical nested scrolling accumulate how much has been scrolled. Start translating the FAB when you have scrolled as much as FAB height:
Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dx, int dy, int[] consumed) {
if (dy > 0 && mTotalDy < 0 || dy < 0 && mTotalDy > 0) {
mTotalDy = 0;
}
mTotalDy += dy;
if ( mTotalDy > child.getHeight()
&& child.getVisibility() == View.VISIBLE) {
//translate to it's height, offscreen, set visbility to gone at end of translation animation
} else if (mTotalDy < 0
&& child.getVisibility() == View.GONE) {
//translate to 0 set visbility to visible at end of translation animation
}
}
When mTotalDy
is greater than FAB height we have scrolling down, when the mTotalDy
we are scrolling up.
You should also take care of nested flinging in onNestedPreFling()
method. Hide the FAB when the velocityY < 0
and show it when velocityY > 0
, all these conditions only when Math.abs(velocityY) > Math.abs(velocityX)
. In other words, only when there is vertical flinging.