Skip to content

Instantly share code, notes, and snippets.

@nmhung
Forked from ValCanBuild/BottomNavigationBehavior.kt
Created September 15, 2018 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nmhung/7921ccec6d29eb3c4a4588c6f12ca4af to your computer and use it in GitHub Desktop.
Save nmhung/7921ccec6d29eb3c4a4588c6f12ca4af to your computer and use it in GitHub Desktop.
Full code of a BottomNavigationBehavior which hides itself on scroll and handles Snackbars and FAB. https://medium.com/@ValCanBuild/scroll-your-bottom-navigation-view-away-with-10-lines-of-code-346f1ed40e9e
// Apply to BottomNavigationView
class BottomNavigationBehavior<V : View>(context: Context, attrs: AttributeSet) :
CoordinatorLayout.Behavior<V>(context, attrs) {
override fun layoutDependsOn(parent: CoordinatorLayout?, child: V, dependency: View?): Boolean {
if (dependency is Snackbar.SnackbarLayout) {
updateSnackbar(child, dependency)
}
return super.layoutDependsOn(parent, child, dependency)
}
override fun onStartNestedScroll(
coordinatorLayout: CoordinatorLayout, child: V, directTargetChild: View, target: View, axes: Int, type: Int
): Boolean {
return axes == ViewCompat.SCROLL_AXIS_VERTICAL
}
override fun onNestedPreScroll(
coordinatorLayout: CoordinatorLayout, child: V, target: View, dx: Int, dy: Int, consumed: IntArray, type: Int
) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type)
child.translationY = max(0f, min(child.height.toFloat(), child.translationY + dy))
}
private fun updateSnackbar(child: View, snackbarLayout: Snackbar.SnackbarLayout) {
if (snackbarLayout.layoutParams is CoordinatorLayout.LayoutParams) {
val params = snackbarLayout.layoutParams as CoordinatorLayout.LayoutParams
params.anchorId = child.id
params.anchorGravity = Gravity.TOP
params.gravity = Gravity.TOP
snackbarLayout.layoutParams = params
}
}
}
// Apply to FAB anchored to BottomNavigationView
class BottomNavigationFABBehavior(context: Context?, attrs: AttributeSet?) : CoordinatorLayout.Behavior<View>(context, attrs) {
override fun layoutDependsOn(parent: CoordinatorLayout?, child: View, dependency: View): Boolean {
return dependency is Snackbar.SnackbarLayout
}
override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
child.translationY = 0f
}
override fun onDependentViewChanged(parent: CoordinatorLayout, child: View, dependency: View): Boolean {
return updateButton(child, dependency)
}
private fun updateButton(child: View, dependency: View): Boolean {
if (dependency is Snackbar.SnackbarLayout) {
val oldTranslation = child.translationY
val height = dependency.height.toFloat()
val newTranslation = dependency.translationY - height
child.translationY = newTranslation
return oldTranslation != newTranslation
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment