我遵循了来自Android开发者指南的指导,并将导航组件
实现到我的应用程序中。
当我需要一些屏幕有或没有工具栏/底部导航栏时,我遇到了一个问题。
Android开发者的示例布局
<androidx.appcompat.widget.Toolbar
.../>
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>
强制我在MainActivity的OnDestinationChanged
回调中隐藏/显示工具栏和底部导航栏:
Forces me to hide/show toolbar/bottomNavBar in OnDestinationChanged
callback in MainActivity:
navController.addOnDestinationChangedListener { _, destination, _ ->
when (destination) {
R.id.topLevelDestination-> {
toolbar.visibility = View.GONE
bottomNav.visibility = View.VISIBLE
}
R.id.lowLevelDestination -> {
toolbar.visibility = View.VISIBLE
bottomNav.visibility = View.GONE
}
当然,如果我这样做,我在看到新的片段之前就会调整我的布局。我的意思是,在片段A上看到底部导航栏消失,在片段A仍然在屏幕上时,在底部导航栏所在的位置看到片段B的部分,之后片段B出现。
如何解决?我需要嵌套导航图吗?
更新:添加了问题的gif动画
视频描述:这是我屏幕的一部分。您可以在视频中看到系统UI、底部导航栏和带有按钮的主要片段。当我点击按钮时,导航图将我导航到一个没有底部导航栏的目标。因此,当OnDestinationChanged时,我执行bottomNavBar.hide()。正如您所看到的,bottomNavBar在我实际导航之前就已经消失了,并且您可以看到底部导航栏消失后我的目标片段的部分可见。这就是问题所在。
onStart()
中隐藏/显示工具栏,而不是依赖于addOnDestinationChangedListener
。你考虑过这种方法吗? - azizbekian