嵌套片段不会显示在ViewPager中

时间:2015-11-07 18:02:07

标签: android android-viewpager android-nested-fragment

ViewPager with 3 pages

附图显示了一个包含3页的ViewPager。每个页面都实现为片段。工具栏上有一个黑色的+图标,用于添加新患者。按下+图标时,应在患者页面内创建一个嵌套片段,以便创建新患者。

在Logcat中,我可以看到嵌套片段的片段生命周期回调被调用到onResume(),但是不会渲染嵌套片段的视图。

我这样做是为了创建嵌套片段并将其插入父片段视图层次结构中:

FrPatientDetails frChild = new FrPatientDetails();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction()
                .replace(R.id.flPatientsPage, frChild);
                transaction.addToBackStack(null)
                .commit();

R.id.flPatientsPage是患者页面中的FrameLayout,我试图插入嵌套片段。

我感觉将子片段插入ViewPager中托管的父片段的方式可能有所不同,但我不清楚该怎么做。

有人能够在这里提供帮助。

更新:为患者页面添加了xml(父级)

<?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>

更新:这是关注@Filo建议的截图: Child fragment view superimposed on the parent fragment

1 个答案:

答案 0 :(得分:2)

总结一下,这是我面临的问题:  活动A包含TabLayoutViewPager(V)。 V包含3页(片段)。其中一个片段(F1)包含一个子片段C.我能够显示F1。当用户点击F1中的按钮时,我无法显示C.

解决方案:V使用FragmentPagerAdapter设置片段。在FragmentPagerAdapter.getItem()内,直接启动F1。相反,启动一个名为F1Wrapper的片段,其中包含FrameLayout。在F1Wrapper.onCreateView()内,创建一个FragmentTransaction向后台添加F1。使用getChildFragmentManager创建交易。将F1Wrapper的FrameLayout作为F1的视图容器传递。

当用户单击F1中的按钮以启动片段C时,在F1Wrapper中创建一个FragmentTransaction.replace(),它将C添加到backstack。使用getChildFragmentManager创建交易。再次,传递F1Wrapper的FrameLayout作为C的视图容器.C现在将替换后台中的F1并将显示。

注意:您无法将ViewPager作为C的视图容器传递。这是问题的关键,也是我们需要使用包装器片段的主要原因。如果将V作为C的视图容器传递,则C.onCreateView()将崩溃。

相关问题