【发布时间】:2019-10-09 17:20:04
【问题描述】:
我正在一个新的 Android 应用上使用导航组件,但我不知道该怎么做
首先,我有我的 MainActivity,我有 main_navigation_graph
主要活动 NavHostFragment
<fragment
android:id="@+id/main_navigation_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/main_navigation_graph"
app:defaultNavHost="true"/>
main_navigation_graph里面有3个片段
这里一切正常。问题是当我到达最后一个片段时,因为在这个片段上,我想根据 BottomNavigationView 输入(暂时)显示一些子片段(在新的 NavHostFragment 内)
最后一个片段:
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#FFF">
<fragment
android:id="@+id/dash_board_navigation"
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="androidx.navigation.fragment.NavHostFragment"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/dash_board_bottom_navigation"
app:navGraph="@navigation/dash_board_navigation_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/dash_board_bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:menu="@menu/main_menu_navigation"
app:labelVisibilityMode="labeled"
android:background="@color/main_menu_background"
app:itemIconTint="@color/main_menu_item_color"
app:itemTextColor="@color/main_menu_item_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
dash_board_navigation_graph
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/dash_board_navigation_graph"
app:startDestination="@id/destination_home_fragment">
<action
android:id="@+id/dash_board_action1"
app:destination="@id/destination_fragment1"/>
<action
android:id="@+id/dash_board_action2"
app:destination="@id/destination_fragment2"/>
<action
android:id="@+id/dash_board_action3"
app:destination="@id/destination_fragment3"/>
<action
android:id="@+id/dash_board_action4"
app:destination="@id/destination_fragment4"/>
<fragment
android:id="@+id/destination_fragment1"
android:name="com.....Fragment1">
</fragment>
<fragment
android:id="@+id/destination_fragment2"
android:name="com.....Fragment2">
</fragment>
<fragment
android:id="@+id/destination_fragment3"
android:name="com.....Fragment3">
</fragment>
<fragment
android:id="@+id/destination_fragment4"
android:name="com.....Fragment4">
</fragment>
当我从最后一个片段触发这些操作时:
findNavController().navigate(R.id.dash_board_action1/2/3)
我得到了这个例外:
java.lang.IllegalArgumentException: navigation destination com.asperitass.mobile:id/dash_board_home_action is unknown to this NavController
我的问题是:我的方法对吗?或者这(具有多级导航)甚至可能吗?或者我应该使用childFragmentManager 来处理这个子导航并在我的片段中膨胀一个 FrameLayout 吗?
【问题讨论】:
标签: android navigation