首页 > 解决方案 > 启用自动隐藏/显示后,BottomNavigationView 不显示

问题描述

我有Activity一个BottomNavigationView内部布局的单曲:

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:menu="@menu/menu_home_bottom_navigation"
            />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我的bottom_avigation变化nav_host FragmentContainerView与碎片。所有这些片段都有NestedScrollViewRecyclerView因为app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior",我bottom_navigation在 scrollDown/scrollUp 上自动隐藏/显示。

我看到了这个问题:Hide/Show bottomNavigationView on Scroll 。我目前正在使用Abhishek Singh给出的答案,但问题不在于这个。

这是我的问题:想象一下FragAFragB两者都有RecyclerViewsFragA项目较少,导致所有项目都适合屏幕且不可滚动。现在,当我从 to 切换FragAFragB然后 scrollDown 时,会bottom_navigation隐藏动画,如果我按下后退按钮,我就再也看不到bottom_navigation了,因为FragA不可滚动,我无法通过滚动使其可见。

我也尝试过bottom_navigation.visibility = View.VisibleFragA onResume但仍然无法正常工作。我认为它以某种方式转化bottom_navigation为底部,因此此代码无济于事。

那么我该如何解决这个问题呢?

标签: androidmaterial-designandroid-animationbottomnavigationview

解决方案


我找到了答案。我没有更改 的visibility属性bottom_navigation,而是编写了两个扩展函数BottomNavigationView来隐藏/显示它:

private fun BottomNavigationView.showUp() {
    animate().setDuration(200L).translationY(0f).withStartAction { visibility = View.VISIBLE }.start()
}

private fun BottomNavigationView.hideDown() {
    animate().setDuration(200L).translationY(height.toFloat()).withEndAction { visibility = View.GONE }.start()
}

现在我有这个onResumeFragA

override onResume() {
    super.onResume()
    bottom_navigation.showUp()
}

推荐阅读