首页 > 解决方案 > 如何在底部导航视图中从另一个片段切换片段?

问题描述

我使用底部导航视图 [1.HomeFragment 2.DashboardFragment 3.ProfileFragment]。我想从 HomeFragment 导航到 DashboardFragment。

这是我使用的代码,但它不起作用,它关闭了应用程序

b3.setOnClickListener(new OnClickListener(){
@override
    public void onClick(View view) {
    Fragment fragment = new DashboardFragment();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.navigation_dashboard, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    }
});

而且我用这种方法也行不通:

getSupportFragmentManager().beginTransaction().replace(R.id.navigation_dashboard, new DashboardFragment()).commit();

标签: androidandroid-fragmentsfragment

解决方案


我强烈推荐使用导航组件。 https://developer.android.com/guide/navigation/navigation-getting-started

  // Java language implementation
  implementation "androidx.navigation:navigation-fragment:$nav_version"
  implementation "androidx.navigation:navigation-ui:$nav_version"

  // Kotlin
  implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
  implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

  // Feature module Support
  implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

创建一个菜单: res/menu/my_navigation_items.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
     <item android:id="@+id/action_home"
          android:title="@string/menu_home"
          android:icon="@drawable/ic_home" />
     <item android:id="@+id/action_dashboard"
          android:title="@string/menu_dashboard"
          android:icon="@drawable/ic_dashboard" />
     <item android:id="@+id/action_profile"
          android:title="@string/menu_profile"
          android:icon="@drawable/ic_profile" />
 </menu>

并使用app:menu="@menu/my_navigation_items"将其分配为 bottomNavigationView 菜单:

<com.google.android.material.bottomnavigation.BottomNavigationView
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schema.android.com/apk/res/res-auto"
     android:id="@+id/navigation"
     android:layout_width="match_parent"
     android:layout_height="56dp"
     android:layout_gravity="start"
     app:menu="@menu/my_navigation_items" />

最后最重要的是,在 androidx.fragment.app.FragmentContainerView 中使用导航图:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_container"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        app:navGraph="@navigation/navigation_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/my_navigation_items"
        app:labelVisibilityMode="labeled"/>

</LinearLayout>

该图将自动实例化您的片段。您应该在 res/navigation/ 中创建它:

<?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"
    app:startDestination="@id/home_fragment">
    <fragment
        android:id="@+id/home_fragment"
        android:name="<yourpackage>.HomeFragment"
        android:label="@string/home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/dashboard_fragment"
        android:name="<yourpackage>.DashboardFragment"
        android:label="@string/informations"
        tools:layout="@layout/fragment_report" />
    <fragment
        android:id="@+id/profile_fragment"
        android:name="<yourpackage>.ProfileFragment"
        android:label="@string/profile"
        tools:layout="@layout/profile_layout" />
</navigation>

推荐阅读