Android Jetpack导航navController问题

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

无法理解为什么它不起作用...另一个模拟项目bnv正常工作...应用内app:startDestination="@+id/navigate_hot"正常运行,但单击bnv项目-则无反应

mainActivity.XML

  <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bttm_nav"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/white"
        android:elevation="4dp"
        app:itemIconSize="35dp"
        app:itemTextColor="@color/bnv_tab_item_foreground"
        app:itemIconTint="@color/bnv_tab_item_foreground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu"/>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <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:navGraph="@navigation/bnv_navigation" /> 

MainActivity.kt]

  override fun onSupportNavigateUp(): Boolean {
        return findNavController(this@ActivityMain,R. id.nav_host_fragment).navigateUp()
    }

    private fun setupNavigation() {
        val navController = findNavController(this@ActivityMain, R.id.nav_host_fragment)
        bttm_nav.setupWithNavController(navController)
    } 

bnv_navigation.XML

<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/navigate_hot">

    <fragment
        android:id="@+id/navigate_news"
        android:name="com.example.fatsale.fragments.NewsFragment"
        android:label="News"
        tools:layout="@layout/fragment_news" />
    <fragment
        android:id="@+id/navigate_hot"
        android:name="com.example.fatsale.fragments.HotFragment"
        android:label="Collection"
        tools:layout="@layout/fragment_hot" />
    <fragment
        android:id="@+id/navigate_profile"
        android:name="com.example.fatsale.fragments.ProfileFragment"
        android:label="Profile"
        tools:layout="@layout/fragment_profile" />

</navigation>

告诉这只驴子需要做什么。

android navigation bottomnavigationview android-jetpack android-jetpack-navigation
1个回答
0
投票

在您用于bottomnavigationview的菜单文件中,每个项目ID必须与在导航图xml文件中添加的片段ID相同。同样在菜单文件中,每个项目都必须为bottomnavigationview中的片段位置no指定OrderInCategory。

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