Android 嵌套片段不会显示在ViewPager中
所附图片显示了一个有3页的ViewPager。每个页面都实现为一个片段。工具栏上有一个黑色+图标,用于添加新患者。按下+图标时,应在患者页面内创建嵌套片段,以允许创建新患者 在Logcat中,我可以看到嵌套片段的片段生命周期回调被调用到Android 嵌套片段不会显示在ViewPager中,android,android-viewpager,android-nested-fragment,Android,Android Viewpager,Android Nested Fragment,所附图片显示了一个有3页的ViewPager。每个页面都实现为一个片段。工具栏上有一个黑色+图标,用于添加新患者。按下+图标时,应在患者页面内创建嵌套片段,以允许创建新患者 在Logcat中,我可以看到嵌套片段的片段生命周期回调被调用到onResume(),但是嵌套片段的视图没有呈现 我这样做是为了创建嵌套片段并将其插入父片段视图层次结构: FrPatientDetails frChild = new FrPatientDetails(); FragmentTransaction transac
onResume()
,但是嵌套片段的视图没有呈现
我这样做是为了创建嵌套片段并将其插入父片段视图层次结构:
FrPatientDetails frChild = new FrPatientDetails();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction()
.replace(R.id.flPatientsPage, frChild);
transaction.addToBackStack(null)
.commit();
R.id.flPatientsPage
是患者页面中的FrameLayout
,我正试图将嵌套片段插入其中
我有一种感觉,将子片段插入到ViewPager
中托管的父片段的方式可能会有所不同,但我不清楚该怎么做
有人能帮忙吗
更新:添加了患者页面的xml(父级)
更新:以下是@Filo建议的截图:
总而言之,我面临的问题是: 有一个活动A包含
表格布局
和查看页面(V)
。V包含3页(片段)。其中一个片段(F1)包含一个子片段C。我能够显示F1。当用户单击F1中的按钮时,我无法显示C
解决方案:V使用FragmentPagerAdapter设置片段。在FragmentPagerAdapter.getItem()内部,不要直接启动F1。相反,启动一个名为F1Wrapper的片段,其中包含一个FrameLayout
。在F1Wrapper.onCreateView()
内部,创建一个碎片事务
将F1添加到backbackback中。使用getChildFragmentManager
创建事务。将F1包装的FrameLayout作为F1的视图容器传递
当用户单击F1中的按钮启动片段C时,在F1Wrapper中创建一个FragmentTransaction.replace()
,将C添加到backbackback中。使用getChildFragmentManager
创建事务。同样,传递F1Wrapper的FrameLayout作为C的视图容器。C现在将在Backback中替换F1并显示
注意:您不能将ViewPager作为C的视图容器传递。这是问题的症结所在,也是我们需要使用包装器片段的主要原因。如果将V作为C的视图容器传递,C.onCreateView()将崩溃。是否尝试使用
add
而不是replace
?是。我尝试了add
和repalce
两种方法,但都给出了相同的结果。加号是否会添加与显示的片段相关的嵌套片段单击加号会导致执行上面的代码片段。可能是与xml相关的flPatientsPage
框架布局问题。你能发布你的父片段布局吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/patients_page_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvHomeViewPatientsPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@color/colorPrimary"
android:dividerHeight="1dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tvEmpty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/black"
android:text="@string/patients_page_empty"
android:fontFamily="sans-serif"
android:gravity="center"
android:textSize="16sp"
/>
<FrameLayout
android:id="@+id/flPatientsPage"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>