0

I have created an app with BottomNavigationView but I want when user scroll the list view then BottomNavigationView should hide. I tried a lot but I am not able to do that. Can anyone help me?

Layout code:

<android.support.design.widget.BottomNavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:menu="@menu/bottom_nav_menu"/>
Saikrishna Rajaraman
  • 3,205
  • 2
  • 16
  • 29
Aakash Jain
  • 87
  • 10

1 Answers1

0

you must create a class to handle the behavior of the BottomNavigationView.

Below I show a code made in Kotlin.

class BottomNavigationBehavior constructor(
    context: Context,
    attrs: AttributeSet? = null
) : CoordinatorLayout.Behavior<BottomNavigationView>(context, attrs) {

  override fun layoutDependsOn(
      parent: CoordinatorLayout,
      child: BottomNavigationView,
      dependency: View
  ): Boolean {
    return dependency is FrameLayout
  }

  override fun onStartNestedScroll(
      coordinatorLayout: CoordinatorLayout,
      child: BottomNavigationView,
      directTargetChild: View,
      target: View,
      axes: Int,
      type: Int
  ): Boolean {
    return axes == ViewCompat.SCROLL_AXIS_VERTICAL
  }
/*this is where bottomnavigation is shown or hidden*/
  override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView,
      target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {
    if (dy < 0) {
      showBottomNavigationView(child)
    } else if (dy > 0) {
      hideBottomNavigationView(child)
    }
  }

  private fun hideBottomNavigationView(view: BottomNavigationView) {
    view.animate().translationY(view.height.toFloat())
  }

  private fun showBottomNavigationView(view: BottomNavigationView) {
    view.animate().translationY(0f)
  }
}

After creating the class, we need to tell the BottomNavigationView which is the class that will control its behavior.

val layoutParams: CoordinatorLayout.LayoutParams = mBottomNavigation.layoutParams as CoordinatorLayout.LayoutParams
    layoutParams.behavior = BottomNavigationBehavior(this)

I hope it helps you.

Juanes30
  • 2,398
  • 2
  • 24
  • 38