如何在向后导航时再次显示 BottomNavigationBar

How to show BottomNavigationBar again on navigate back

我使用 BottomNavigationBarHideBottomViewOnScrollBehavior 来在用户向下滚动时隐藏它并在用户向上滚动时显示它。这很好用。

但是当 BottomNavigationBar 由于用户向下滚动并通过后退按钮导航返回时,我如何才能再次显示它?

目前我的 BottomNavigationView 保持隐藏状态。

我正在使用支持库28.0.0

也许有人对此有更好的解决方案,但现在我想出了以下办法。

在我的 SingleActivity 应用程序的 MainActivity 中,我添加了以下函数来模拟向上滚动:

fun ensureBottomNavigation() {
    if(bottomNavigationView.translationY != 0f) {
        val layoutParams = bottomNavigationView.layoutParams as CoordinatorLayout.LayoutParams
        val behavior = layoutParams.behavior as HideBottomViewOnScrollBehavior

        behavior.onNestedScroll(container, bottomNavigationView, host_fragment.view!!, 0, -1, 0, 0, 0)
    }
}

在我的应用程序的每个片段中,我都在 onResume() 中调用此函数,如下所示:

override fun onResume() {
    super.onResume()

    // Ensure that bottom navigation view is visible onResume()
    (activity as MainActivity).ensureBottomNavigation()
}