android - 从嵌套的 nav_graph 导航到主 nav_graph 中的特定片段以全屏打开它
问题描述
第一个矩形是放置 bottomNavigationView 的容器。第二个是菜单片段打开的地方,我使用第二个 nav_graph 来处理菜单
问题是我无法在全屏上从嵌套的 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" />
</androidx.constraintlayout.widget.ConstraintLayout>
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" />
菜单.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"/>
解决方案
为了从嵌套图导航到主图上的特定目的地,我使用了这种方法
((parentFragment as NavHostFragment).parentFragment as {FragmentFromWhichYouNavigating}).findNavController() .navigate(R.id.newsFirstFragment)
推荐阅读
- swift - 无法转换“ClosedRange”类型的值
' 到预期的参数类型 'Range<_>' - android - ImageView 显示不正确
- python - 如何结束模块定义?
- c# - 无法从字典中获取值
- python - 如何执行python文件?
- python - Django 错误:“您正在尝试在没有默认值的情况下将不可为空的字段‘作者’添加到条目中”
- c# - 如何在 AspNet Core gRPC Service 中导入 protobuf 知名类型
- php - 我的问题是关于用 php 创建一个乘法表
- r - R:path.expand(路径)中的错误:无效的“路径”参数
- c++ - 转换指南