android - 使用 view pager2 和嵌套导航正确使用导航组件

标签 android androidx android-architecture-navigation

我正在尝试使用 ViewPager2 + FragmentStateAdapter + 导航组件构建以下 View 结构/导航。

前提条件:单一 Activity 架构,带有一个导航图

1. fragment A 持有一个 View 寻呼机。 View pager 使用 FragmentStateAdapter。

2. fragment B 通过 FragmentStateAdapter ( View 寻呼机中的“生活”)实例化。

3. fragment C - 应该从 Fragment B 导航到。 --> 这就是问题所在。

方法 1:ViewPager2 + FragmentStateAdapter + 从 Fragment B 声明的导航

    <fragment
        android:id="@+id/fragmentA"
        android:name="com.abc.FragmentA"
        android:label="FragmentA" />

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.abc.FragmentB"
        android:label="FragmentB">
        <action
            android:id="@+id/to_fragmentC"
            app:destination="@id/fragmentC" />
    </fragment>

    <fragment
        android:id="@+id/fragmentC"
        android:name="com.abc.FragmentC"
        android:label="FragmentC" />

fragment B执行:
 FragmentBDirections
            .toFragmentC()
            .let { findNavController().navigate(it) }

结果 :
App crash
java.lang.IllegalArgumentException: navigation destination com.abc:id/to_fragmentC is unknown to this NavController

方法 2:ViewPager2 + FragmentStateAdapter + 从 Fragment A 声明的导航
    <fragment
        android:id="@+id/fragmentA"
        android:name="com.abc.FragmentA"
        android:label="FragmentA" >
        <action
            android:id="@+id/to_fragmentC"
            app:destination="@id/fragmentC" />
    </fragment>

    <fragment
        android:id="@+id/fragmentB"
        android:name="com.abc.FragmentB"
        android:label="FragmentB">
    </fragment>

    <fragment
        android:id="@+id/fragmentC"
        android:name="com.abc.FragmentC"
        android:label="FragmentC" />

fragment B执行:
 FragmentADirections
            .toFragmentC()
            .let { findNavController().navigate(it) }

结果:
App navigates to FragmentC, but when i hit the back button , it crashes with :
java.lang.IllegalArgumentException
        at androidx.core.util.Preconditions.checkArgument(Preconditions.java:36)
        at androidx.viewpager2.adapter.FragmentStateAdapter.onAttachedToRecyclerView(FragmentStateAdapter.java:132)
        at androidx.recyclerview.widget.RecyclerView.setAdapterInternal(RecyclerView.java:1209)
        at androidx.recyclerview.widget.RecyclerView.setAdapter(RecyclerView.java:1161)
        at androidx.viewpager2.widget.ViewPager2.setAdapter(ViewPager2.java:461)
        at com.abc.FragmentA.viewCreated(FragmentA.kt:69)


方法 3:ViewPager + FragmentStatePagerAdapter(已弃用)+ 从 Fragment B 声明的导航

结果与方法 1 相同。

方法 4:ViewPager + FragmentStatePagerAdapter(已弃用)+ 从 Fragment A 声明的导航

这个确实有效。此外,导航返回工作正常。

这里的问题是:
  • 必须为 FragmentB 的每个父 fragment 定义导航 -> 不可扩展
  • 使用已弃用的适配器

  • 如果有人知道这个问题的一些优雅的解决方案,我会很高兴任何提示。

    谢谢

    最佳答案

    您不需要将 Fragment B 声明为图形中的目的地,因为您从不使用 NavController 导航到它。您可以使用 而不是使用操作目的地 ID 在 Fragment B 中实现导航,如 findNavController().navigate(R.id.fragmentC) . findNavController方法会找到父 fragment 的 navController 来执行导航。

    关于android - 使用 view pager2 和嵌套导航正确使用导航组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61638513/

    相关文章:

    java - 如何使我更改的数据在使用viewmodel后出现

    Android X 向后兼容性

    android - 我如何知道图像是否在缓冲区中旋转?

    java - 使用 NavigationComponent 在另一个 DialogFragment 的 navBackStackEntry 观察者结果中显示 DialogFragment

    android - 导航组件 : Get Activity Result

    Android Studio Gradle 同步/构建性能

    android - 初始化 Android(服务器)和 pc(客户端)上的 bluecove 之间的蓝牙连接

    android - React-Native 最低 Android API 级别

    android - Jetpack 撰写 : Could not set unknown property 'useIR'

    android - 在通过导航组件添加到 Action 的动画完成之前,防止点击 View