更新时间:2023-12-06 08:08:10
您正在使用 getChildFragmentManager
获取正确的嵌套片段。
You are using getChildFragmentManager
for nested fragments which is correct.
您正在做什么:
在您的情况下,片段要附加到(孩子的)viewpager,但此处viewpager没有附加到其父视图(仍然分离)。
In your case when the fragments wants to be attached to the (child's) viewpager but here the viewpager is not attached to its parent (still detached).
您应该做什么:
创建自定义 ViewPager
覆盖其 onAttachedToWindow
并执行惰性设置适配器。
Create a custom ViewPager
override its onAttachedToWindow
and do the lazy set adapter.
有关详细信息看看 IllegalArgumentException:无视图找到片段ID的内容-ViewPager中的ViewPager 。
更新:
如果要使用 getFragmentManager
或 getSupportFragmentManager
来更改的父类, PagerAdapter
从 android.support.v4.app.FragmentPagerAdapter
到 android.support.v4.app.FragmentStatePagerAdapter
。
If you want to use getFragmentManager
or getSupportFragmentManager
than change the parent class of your PagerAdapter
from android.support.v4.app.FragmentPagerAdapter
to android.support.v4.app.FragmentStatePagerAdapter
.