gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:27:53 26 4
gpt4 key购买 nike

我正在尝试使用 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com