【问题标题】:Navigation Component with all fragments or some activities?具有所有片段或某些活动的导航组件?
【发布时间】:2020-03-11 03:43:07
【问题描述】:

我正在制作一个核心具有底部导航视图的应用。当我需要导航到不应该有也不显示底部导航视图的页面时,就会出现问题。如果我使用一个片段,底部导航仍然不会显示,因为它是在主要活动的 xml 中声明的:

主要活动 XML

<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>

我的问题是:我应该使用带有自己的 NavHostFragment 和图表的新活动吗?或者也许使用嵌套导航并继续使用片段?如果我使用后者,我将如何隐藏底部导航视图?

【问题讨论】:

    标签: android navigation android-jetpack-navigation


    【解决方案1】:

    根据Listen for navigation events documentation

    例如,您可能有一些常见的 UI 元素,您打算在应用的某些区域显示这些元素,而在其他区域隐藏它们。使用您自己的OnDestinationChangedListener,您可以根据目标目的地选择性地显示或隐藏这些 UI 元素

    所以是的,当您移动到某些目的地时,您可以选择性地显示或隐藏 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
        }
    }
    

    【讨论】:

    • 我在阅读文档时错过了该部分。不过谢谢你!看来他们真的什么都想好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多