5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Android】BottomNavigationをスクロール時に表示・非表示する

Posted at

スクロールでBottom navigationを隠したく、調べたのでメモ。
ヘルパー的なものを自前で用意する必要があるみたい。

BottomNavigationScrollingBehavior.kt
class BottomNavigationScrollingBehavior<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
        }
    }
}

layout.xml
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:itemBackground="@color/bottomNavigationItemBackground"
        app:itemIconTint="@color/bottom_navigation"
        app:itemTextColor="@color/bottom_navigation"
        app:layout_behavior="us.sample.presentation.home.BottomNavigationScrollingBehavior"
        app:menu="@menu/bottom_navigation">

    </android.support.design.widget.BottomNavigationView>

レイアウト側でlayout_behaviorを設定してあげればok

参考

このサンプルは動きがカクカクする
https://stackoverflow.com/questions/44777869/hide-show-bottomnavigationview-on-scroll

こっちは滑らか & FABとかSnackbarにも対応してる
https://android.jlelse.eu/scroll-your-bottom-navigation-view-away-with-10-lines-of-code-346f1ed40e9e
https://gist.github.com/ValCanBuild/4bbcb47576e875a17440cf2c438e030a

Material Design Guideline
https://material.io/design/components/bottom-navigation.html#behavior

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?