首页 > 解决方案 > 带有导航编辑器的 BottomNavigationView 面临片段不变的问题

问题描述

我正在尝试将 BottomNavigationView 与导航编辑器一起使用。我已经按照给定的方式实现了所有内容,但只显示主页,当我更改选项卡时,它不会更改片段。

这是主要活动代码:

public class MainActivity extends AppCompatActivity {
    BottomNavigationView mainBottomNavigation;

    NavController navController;
    NavHostFragment navHostFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainBottomNavigation = findViewById(R.id.bottom_navigation);
        navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(mainBottomNavigation, Navigation.findNavController(this,R.id.nav_host_fragment));
    } }

这是主要的活动 xml 文件:

<?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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <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_toTopOf="@+id/bottom_navigation"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        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/navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是菜单文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_location"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_location" />

    <item
        android:id="@+id/navigation_sightseeing"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_sightseeing" />

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_gallery"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_gallery" />

    <item
        android:id="@+id/navigation_contact_us"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_contact_us" />

</menu>

这是我创建的 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/nav_graph"
    app:startDestination="@id/navigation_home">
    <fragment
        android:id="@+id/navigation_location"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_location"
        tools:layout="@layout/fragment_location" />
    <fragment
        android:id="@+id/navigation_sightseeing"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_sightseeing"
        tools:layout="@layout/fragment_sight_seeing" />
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/navigation_gallery"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_gallery"
        tools:layout="@layout/fragment_gallery" />
    <fragment
        android:id="@+id/navigation_contact_us"
        android:name="com.wedding.rashmilind.HomeFragment"
        android:label="fragment_contact_us"
        tools:layout="@layout/fragment_contact_us" />
</navigation>

标签: androidbottomnavigationviewandroid-jetpackandroid-jetpack-navigation

解决方案


对于导航,您需要调用 NavController 的navigate()方法。


导航到目的地是使用 来完成的NavController,该对象在NavHost. 每个 NavHost 都有自己对应的 NavController。

要检索片段、活动或视图的 NavController,请使用以下方法之一:

NavHostFragment.findNavController(Fragment)
// -- or --
Navigation.findNavController(Activity, @IdRes int viewId)
// -- or --
Navigation.findNavController(View)

检索到 NavController 后,使用其navigate()方法导航到目的地。navigate() 方法接受动作或目的地的资源 ID。

然后,在导航按钮上设置以下内容onClick

Navigation.findNavController(view).navigate(R.id.viewTransactionsAction);

参考:官方文档


推荐阅读