带导航编辑器的BottomNavigationView面临片段不变的问题

问题描述 投票:1回答:1

我正在尝试将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>
android bottomnavigationview android-jetpack android-jetpack-navigation
1个回答
0
投票

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


导航到目的地是使用NavController完成的,NavHost是一个在NavHostFragment.findNavController(Fragment) // -- or -- Navigation.findNavController(Activity, @IdRes int viewId) // -- or -- Navigation.findNavController(View) 内管理应用程序导航的对象。每个NavHost都有自己的相应NavController。

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

navigate()

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

然后,在导航按钮的Navigation.findNavController(view).navigate(R.id.viewTransactionsAction); 上设置以下内容:

official documentation

参考:qazxswpoi

© www.soinside.com 2019 - 2024. All rights reserved.