动态添加片段到片段中 [英] Dynamically add fragment into fragment

查看:29
本文介绍了动态添加片段到片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I haven't been able to find a way how to dynamically add fragment into existing dynamically added fragment. Do you know, if it is possible?

我是这样生成片段的:

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 片段中?

How to add into FRAG4_TAG fragment another one?

编辑 2:

我硬编码了它的 id 以便将来能够使用它(其中 ll 是我在 XML 中的线性布局):

I hard coded it's id to be able to work with it in future (where ll is my linearLayout in XML):

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

ll.addView(frml4);

推荐答案

我假设您遇到的问题是没有膨胀的视图来添加片段,因为原始片段 FRAG4_TAG 尚未膨胀在您尝试添加之前.

I assume the problem that you are running into is that there is not an inflated view to add the fragment to because the original fragment, FRAG4_TAG, has not been inflated before you are trying to add it.

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

You can pass enough information to FRAG4_TAG in the Arguments to let it know that it should create and add a fragment (or what all fragments you need it to have) to itself during it's onCreateView, after the view has been inflated...

活动的布局...

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

活动...

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();

    }
}

片段的布局...

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

片段...

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;
    }
}

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

This example covers the case where you need to dynamically add fragments to a fragment that HAS NOT already been inflated and added to the hierarchy. Adding a fragment to a fragment that HAS already been inflated and added to the hierarchy is as simple as just specifying the target fragments container that you want to add to by ID like you would normally.

这篇关于动态添加片段到片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆