从嵌套的 nav_graph 导航到主 nav_graph 中的特定片段以全屏打开它

时间:2021-06-23 20:58:32

标签: android android-jetpack-navigation

the first rectangle is a container where bottomNavigationView is placed. The second one is a place where menu fragments open, I used the second nav_graph to work with menu

问题是我无法在全屏上从嵌套的 nav_graph 打开片段

这是容器片段

<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/common_bg"
tools:context=".ui.news.NavigationContainerFragment">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:src="@drawable/ic_logo"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ImageView
    android:id="@+id/iv_settings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:src="@drawable/ic_settings"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<fragment
    android:id="@+id/bottom_nav_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/bottom_nav"
    app:layout_constraintTop_toBottomOf="@id/iv_settings"
    app:navGraph="@navigation/nav_view_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bottom_nab_bg"
    app:itemBackground="@drawable/nav_item_drawable"
    app:itemIconTint="@color/bottom_nav_colors"
    app:itemTextColor="@color/bottom_nav_colors"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/menu" />

Nav_graph 嵌套在其中

<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_view_graph"
app:startDestination="@id/newsListFragment">

<fragment
    android:id="@+id/newsListFragment"
    tools:layout="@layout/fragment_news_list"
    android:name="com.winlineapp.ui.news.NewsListFragment"
    android:label="NewsListFragment" />
<fragment
    android:id="@+id/tableFragment"
    android:name="com.winlineapp.ui.TableFragment"
    android:label="fragment_calendar"
    tools:layout="@layout/fragment_table" />
<fragment
    android:id="@+id/calendarFragment"
    android:name="com.winlineapp.ui.CalendarFragment"
    android:label="fragment_calendar"
    tools:layout="@layout/fragment_calendar" />
<fragment
    android:id="@+id/statisticsFragment"
    android:name="com.winlineapp.ui.StatisticsFragment"
    android:label="fragment_statistics"
    tools:layout="@layout/fragment_statistics" />

menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@id/newsListFragment"
    android:icon="@drawable/ic_feed"
    android:checked="true"
    android:title="Feed"/>

<item android:id="@id/tableFragment"
    android:icon="@drawable/ic_table"
    android:title="Table"/>

<item android:id="@id/calendarFragment"
    android:icon="@drawable/ic_calendar"
    android:title="Calendar"/>

<item android:id="@id/statisticsFragment"
    android:icon="@drawable/ic_statistics"
    android:title="Statistics"/>

1 个答案:

答案 0 :(得分:0)

为了从嵌套图导航到主图上的特定目的地,我使用了这种方法

((parentFragment as NavHostFragment).parentFragment as {FragmentFromWhichYouNavigating}).findNavController() .navigate(R.id.newsFirstFragment)