【问题标题】:Bottom Navigation View doesn't work after going to a fragment not inside the bottom navigation view and coming back转到不在底部导航视图内的片段并返回后,底部导航视图不起作用
【发布时间】:2021-08-18 16:22:35
【问题描述】:

所以我的应用架构有点独特,顺便说一句,我正在使用导航组件。

它有一个与 NavHostFragment 关联的主要活动。 NavHostFragment 有与之关联的导航图,它有两个片段,主片段(作为起始目的地)和设置片段(可通过工具栏中的菜单项访问)。 现在 Main Fragments 充当另一个 NavHostFragment 的另一个主机。所以看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.fragments.MainFragment">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/child_nav_host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/child_nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation_bar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

请注意,在 Main Fragment 的底部有一个底部导航栏,但在 Main Activity 中没有这样的底部导航栏。另请注意,Main Fragment 有另一个 NavHostFragment 指向不同的 Navigation Graph。

这是 Main Activity 的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.MainActivity">

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/materialToolbar"
        style="@style/Widget.MaterialComponents.Toolbar.Primary"
        android:layout_width="0dp"
        android:layout_height="?actionBarSize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/parent_nav_host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/materialToolbar"
        app:navGraph="@navigation/main_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是 main_nav_graph:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_nav_graph"
    app:startDestination="@id/main_fragment">
    <fragment
        android:id="@+id/settings_fragment"
        android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.SettingsFragment"
        android:label="Settings"
        tools:layout="@layout/fragment_settings" />
    <fragment
        android:id="@+id/main_fragment"
        android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.MainFragment"
        android:label="Welcome"
        tools:layout="@layout/fragment_main" />
</navigation>

这是 child_nav_graph

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_nav_graph"
    app:startDestination="@id/aws_fragment">

    <fragment
        android:id="@+id/aws_fragment"
        android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.AwsFragment"
        android:label="AWS"
        tools:layout="@layout/fragment_aws" />

    <fragment
        android:id="@+id/azure_fragment"
        android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.AzureFragment"
        android:label="Azure"
        tools:layout="@layout/fragment_azure" />

    <fragment
        android:id="@+id/gcp_fragment"
        android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.GcpFragment"
        android:label="GCP"
        tools:layout="@layout/fragment_gcp" />
</navigation>

如您所见,Main Fragment 承载了与底部导航栏映射的其他三个 Fragment。我使用这种方法是因为我希望我的设置片段在导航到它时隐藏底部导航栏。现在,底部导航栏显示在我希望它们显示的所有三个片段中,但不像我期望的那样显示在设置片段中。

现在问题来了,当我打开应用程序时,会显示主片段。主片段包含底部导航栏,当我选择底部导航栏中的一项时,它会正确导航到正确的片段。但是当我去设置片段然后回来时,底部导航栏停止工作。这是显示该行为的视频:https://imgur.com/RpQZmhF

这是 MainActivity.kt:https://gist.github.com/ac65ae323ca771d064562d18d9fac880 这是 MainFragment.kt:https://gist.github.com/c700e33b4f04e078abf8c94db9bb6faf

【问题讨论】:

  • 如果您使用的是导航组件,那么您可以设置条件来隐藏/显示底部导航视图以显示在哪个片段中。
  • 这不太理想,因为我希望显示一个动画,让它感觉设置片段覆盖了底部导航栏。
  • 您也可以将动画应用于片段。或者您可以将动画设置为 BottomView 以使用动画隐藏
  • 但是我目前的方法有什么问题?我不明白为什么它不起作用
  • 您的主导航控制器似乎已重置,或者我们需要对其进行调试以找到实际原因。

标签: android android-fragments bottomnavigationview android-architecture-navigation android-bottomnav


【解决方案1】:

问题出在我的 MainFragment 中

    val bottomNavBar by lazy {
        binding.bottomNavigationBar
    }

而不是这样做,我应该在任何地方使用binding.bottomNavigationBar 直接引用 bnv。这样就解决了。

【讨论】:

【解决方案2】:

开始新片段的事务时,不要添加添加

addtobackstack()

改用这个

if (my_cart == null) {
                my_cart = new My_Cart();
                fragmentManager.beginTransaction()
                        .hide(currentFragment)
                        .add(R.id.main_fragment, my_cart)
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                        .commit();
            } else {
                fragmentManager.beginTransaction().hide(currentFragment).show(my_cart).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
            }
            currentFragment = my_cart;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多