android - 将 fragment 动态添加到 fragment 中

标签 android android-fragments

我一直无法找到如何将 fragment 动态添加到现有动态添加的 fragment 中的方法。你知道,如果可能的话?

我是这样生成 fragment 的:

FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction xact = fragMgr.beginTransaction();

if(null == fragMgr.findFragmentByTag(FRAG1_TAG)) {
   xact.add(10101010, new DateTime(), FRAG1_TAG); 
}

if(null == fragMgr.findFragmentByTag(FRAG4_TAG)) {
   xact.add(7777, new loginForm(), FRAG4_TAG);

}

xact.commit(); 

如何在FRAG4_TAG fragment 中添加另一个?

编辑2:

我对它的 id 进行了硬编码,以便将来能够使用它(其中 ll 是我在 XML 中的 linearLayout):

FrameLayout frml4 = (FrameLayout)inflater.inflate(R.layout.frame,null);
frml4.setId(7777);
frml4.setBackgroundColor(Color.YELLOW);

ll.addView(frml4);

最佳答案

我假设您遇到的问题是没有可添加 fragment 的膨胀 View ,因为原始 fragment FRAG4_TAG 在您尝试添加之前尚未膨胀。

您可以在 Arguments 中向 FRAG4_TAG 传递足够的信息,让它知道在 View 膨胀后,它应该在 onCreateView 期间创建并添加一个 fragment (或您需要它拥有的所有 fragment )。 .

Activity 的布局...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="MyActivity"/>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/main_frag_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

Activity ...

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        Fragment fragOne = new MyFragment();
        Bundle arguments = new Bundle();
        arguments.putBoolean("shouldYouCreateAChildFragment", true);
        fragOne.setArguments(arguments);
        ft.add(R.id.main_frag_container, fragOne);
        ft.commit();

    }
}

fragment 的布局...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp">
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Some fragment"/>

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/frag_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

fragment ...

public class MyFragment extends Fragment {
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.frag_layout, container, false);

        boolean shouldCreateChild = getArguments().getBoolean("shouldYouCreateAChildFragment");

        if (shouldCreateChild) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();

            fm.beginTransaction();
            Fragment fragTwo = new MyFragment();
            Bundle arguments = new Bundle();
            arguments.putBoolean("shouldYouCreateAChildFragment", false);
            fragTwo.setArguments(arguments);
            ft.add(R.id.frag_container, fragTwo);
            ft.commit();

        }

        return layout;
    }
}

此示例涵盖了您需要将 fragment 动态添加到尚未膨胀并添加到层次结构的 fragment 的情况。将 fragment 添加到已经膨胀并添加到层次结构中的 fragment 就像​​通常一样通过 ID 指定要添加到的目标 fragment 容器一样简单。

关于android - 将 fragment 动态添加到 fragment 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7915413/

相关文章:

java - Android getParentFragment() 在 fragment 内的 ViewPager 中返回 null

android - 在 Android Fragment 中滑动刷新布局

java - 限制基于 Java 的 Web 服务器 TLS 协议(protocol)以供 Wireshark 检查

java - 数组中的 Firebase 数据检索

android - 获取随机数据列表的算法/工作方法

android - 增加 Android 模拟器对一般/应用程序操作的响应时间

java - 如何使变量在所有 fragment 之间保持 Activity 状态

android - WebView - Youtube 视频在后台播放并最小化

java - 基于 fragment 的 ViewPager 作为 RecyclerView 项目产生空对象引用

android - 如何在同一个选项卡中的 fragment 之间切换?