包含所有片段或某些活动的导航组件?
Navigation Component with all fragments or some activities?
我正在开发一个应用程序,其核心是底部导航视图。当我需要导航到一个不应有或显示底部导航视图的页面时,就会出现问题。如果我使用一个片段,底部导航不会仍然显示,因为它是在主 activity 的 xml:
中声明的
主要ActivityXML
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的问题是:我应该使用新的 activity 及其自己的 NavHostFragment 和图形吗?或者也许使用嵌套导航并继续使用片段?如果选择后者,我将如何隐藏底部导航视图?
根据 Listen for navigation events documentation:
As an example, you might have common UI elements that you intend to show in some areas of your app while hiding them in others. Using your own OnDestinationChangedListener
, you can selectively show or hide these UI elements based on the target destination
所以是的,您可以有选择地显示或隐藏 activity 的 UI 的元素,例如当您移动到某些目的地时 BottomNavigationView
:
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.full_screen_destination) {
nav_view.visibility = View.GONE
} else {
nav_view.visibility = View.VISIBLE
}
}
我正在开发一个应用程序,其核心是底部导航视图。当我需要导航到一个不应有或显示底部导航视图的页面时,就会出现问题。如果我使用一个片段,底部导航不会仍然显示,因为它是在主 activity 的 xml:
中声明的主要ActivityXML
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的问题是:我应该使用新的 activity 及其自己的 NavHostFragment 和图形吗?或者也许使用嵌套导航并继续使用片段?如果选择后者,我将如何隐藏底部导航视图?
根据 Listen for navigation events documentation:
As an example, you might have common UI elements that you intend to show in some areas of your app while hiding them in others. Using your own
OnDestinationChangedListener
, you can selectively show or hide these UI elements based on the target destination
所以是的,您可以有选择地显示或隐藏 activity 的 UI 的元素,例如当您移动到某些目的地时 BottomNavigationView
:
navController.addOnDestinationChangedListener { _, destination, _ ->
if(destination.id == R.id.full_screen_destination) {
nav_view.visibility = View.GONE
} else {
nav_view.visibility = View.VISIBLE
}
}