Skip to content

Instantly share code, notes, and snippets.

@sabiou
Forked from nikhilpanju/ToolbarBehavior.kt
Created December 16, 2019 23:46
Show Gist options
  • Save sabiou/d72ef26c1ff0bc59293fbbb9433191d9 to your computer and use it in GitHub Desktop.
Save sabiou/d72ef26c1ff0bc59293fbbb9433191d9 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