gpt4 book ai didi

android - 嵌套 fragment 未显示在 ViewPager 中

转载 作者:行者123 更新时间:2023-11-29 20:18:00 30 4
gpt4 key购买 nike

ViewPager with 3 pages

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

在 Logcat 中,我可以看到嵌套 fragment 的 fragment 生命周期回调一直被调用到 onResume(),但是嵌套 fragment 的 View 没有呈现。

我这样做是为了创建嵌套 fragment 并将其插入到父 fragment View 层次结构中:

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

R.id.flPatientsPage 是 Patients 页面中的一个 FrameLayout,我正尝试将嵌套 fragment 插入其中。

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

有人可以帮忙吗

更新:为患者页面(父)添加了 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

最佳答案

总而言之,这是我面临的问题: 有一个包含 TabLayoutViewPager(V) 的 Activity A。 V 包含 3 页( fragment )。其中一个 fragment (F1)包含一个子 fragment C。我能够显示 F1。当用户单击 F1 中的按钮时,我无法显示 C。

解决方案:V 使用 FragmentPagerAdapter 来设置 fragment 。在 FragmentPagerAdapter.getItem() 中,不要直接启动 F1。相反,启动一个名为 F1Wrapper 的 fragment ,其中包含一个 FrameLayout。在 F1Wrapper.onCreateView() 中,创建一个 FragmentTransaction,将 F1 添加到后台堆栈。使用 getChildFragmentManager 创建事务。传递 F1Wrapper 的 FrameLayout 作为 F1 的 View 容器。

当用户单击 F1 中的按钮启动 fragment C 时,在 F1Wrapper 中创建一个 FragmentTransaction.replace() 将 C 添加到后台堆栈。使用 getChildFragmentManager 创建事务。同样,将 F1Wrapper 的 FrameLayout 作为 C 的 View 容器传递。C 现在将在后台堆栈中替换 F1 并显示。

注意:您不能将 ViewPager 作为 C 的 View 容器传递。这是问题的症结所在,也是我们需要使用包装器 fragment 的主要原因。如果您将 V 作为 C 的 View 容器传递,C.onCreateView() 将崩溃。

关于android - 嵌套 fragment 未显示在 ViewPager 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585968/

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