Skip to content

Instantly share code, notes, and snippets.

@bitsydarel
Created February 15, 2018 18:30
Show Gist options
  • Save bitsydarel/a57059ae42e8b99bf8cb306448d5e6b9 to your computer and use it in GitHub Desktop.
Save bitsydarel/a57059ae42e8b99bf8cb306448d5e6b9 to your computer and use it in GitHub Desktop.
Layout behavior for Bottom Navigation View to hide and show on scroll. Root ViewGroup should be CoordinatorLayout.
class BottomNavigationViewBehaviour(context: Context, attrs: AttributeSet) : CoordinatorLayout.Behavior<BottomNavigationView>(context, attrs) {
private var height: Int = 0
override fun onLayoutChild(parent: CoordinatorLayout?, child: BottomNavigationView?, layoutDirection: Int): Boolean {
height = child!!.height
return super.onLayoutChild(parent, child, layoutDirection)
}
override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView, directTargetChild: View, target: View, axes: Int, type: Int): Boolean {
return axes == ViewCompat.SCROLL_AXIS_VERTICAL
}
override fun onNestedScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView, target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int) {
if (dyConsumed > 0) {
child.visibility = View.INVISIBLE
slideDown(child)
} else if (dyConsumed < 0) {
child.visibility = View.VISIBLE
slideUp(child)
}
}
private fun slideUp(child: BottomNavigationView) {
child.clearAnimation()
child.animate().translationY(0f).duration = 200
}
private fun slideDown(child: BottomNavigationView) {
child.clearAnimation()
child.animate().translationY(height.toFloat()).duration = 200
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment