【发布时间】:2020-10-08 10:47:28
【问题描述】:
我关注guide from android.developers 并在我的应用程序中实现了navigation component。
当我需要一些屏幕带有或不带有工具栏/底部导航栏时,我偶然发现了一个问题。
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 回调中隐藏/显示工具栏/bottomNavBar:
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 消失了,您可以看到在 bottomnavBar 消失后我的目标片段的一部分可见。这就是问题所在。
【问题讨论】:
-
您可以在每个片段的
onStart()中隐藏/显示工具栏,而不是依赖addOnDestinationChangedListener。您考虑过这种方法吗? -
@azizbekian 是的,我做到了。一样的效果。我的问题是我在 mainactivity_layout 中有 1 个片段容器用于所有片段,有或没有工具栏和底栏。
-
您可以在 Fragment 和 Activity 之间使用共享 ViewModel,只有一个
MutableLiveData<Boolean>来设置 Toolbar 的可见性,并在 Fragments 的相应生命周期方法 onCreateView、onViewCreated、onStart 或 onResume 中设置它跨度> -
@Thracian 实际上,我已经知道一个问题的答案。这是一个嵌套的导航组件图。当您有一个带有工具栏和导航栏的 root_fragment_view 和一个没有时,它们在导航时不会显示/隐藏。没有它,我看不到如何解决问题的方法
-
不管它是否是嵌套图,您都可以使用 ViewModel 访问这些片段中的任何数据,并在片段的任何生命周期方法中设置它的 liveData 以更改视图的属性。我创建了一些示例,包括 ViewPager 的
NavHostFragment的子片段的嵌套图,在底部栏的片段内设置当前图的导航或设置 viewpager 的可见性(这个有一些问题)。如果您愿意,可以查看here
标签: android navigation android-toolbar bottomnavigationview android-architecture-navigation