Skip to content

Instantly share code, notes, and snippets.

@nikhilpanju
Last active January 17, 2020 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nikhilpanju/0013aa253c50b23b6c0be802e93eec24 to your computer and use it in GitHub Desktop.
Save nikhilpanju/0013aa253c50b23b6c0be802e93eec24 to your computer and use it in GitHub Desktop.
class ToolbarBehavior : CoordinatorLayout.Behavior<AppBarLayout>() {
/** Consume if vertical scroll because we don't care about other scrolls */
override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout,
directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
getViews(child)
return axes == ViewCompat.SCROLL_AXIS_VERTICAL ||
super.onStartNestedScroll(coordinatorLayout, child, directTargetChild, target, axes, type)
}
/** Perform actual animation by determining the dY amount */
override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: AppBarLayout, target: View,
dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int,
type: Int, consumed: IntArray) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed)
getViews(child)
if (dyConsumed > 0) {
// RecyclerView is scrolling down so shrink toolbar and translate
// drawerIcon using the dyConsumed variable
} else if (dyUnconsumed < 0) {
// RecyclerView has reached the top and user is scrolling up more to reveal toolbar
// So use the dyUnconsumed variable to bring back the toolbar to resting position
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment