动态添加片段插入片段 [英] Dynamically add fragment into fragment

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

问题描述

我一直没能找到一种方法如何动态地添加片段到现有的动态添加的片段。你知不知道,是否有可能?

我生成片段是这样的:

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

如果(空== fragMgr.findFragmentByTag(FRAG1_TAG)){
   xact.add(10101010,新的日期时间(),FRAG1_TAG);
}

如果(空== fragMgr.findFragmentByTag(FRAG4_TAG)){
   xact.add(7777,新的登录表单(),FRAG4_TAG);

}

xact.commit();
 

如何添加到FRAG4_TAG片段另一个呢?

EDIT2:

我很难$ C $光盘,它的ID能够与它的工作在未来(其中LL是我在XML的LinearLayout):

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

ll.addView(frml4);
 

解决方案

我假设你正在运行到的问题是,没有一个膨胀欣赏到片段添加到,因为原来的片段,FRAG4_TAG,并没有被夸大之前您要添加。

您可以传递足够的信息来FRAG4_TAG的参数,让它知道它应该创建并添加一个片段(或者你需要它拥有的所有片段)本身在它的onCreateView,该观点已经膨胀以后。

该活动的布局...

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=MyActivity/>

    <的LinearLayout
        机器人:方向=垂直
        机器人:ID =@ + ID / main_frag_container
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
 

该活动...

 公共类MyActivity延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.main);

        FragmentManager FM = getFragmentManager();
        FragmentTransaction英尺= fm.beginTransaction();

        fm.beginTransaction();
        片段fragOne =新MyFragment();
        捆绑参数=新包();
        arguments.putBoolean(shouldYouCreateAChildFragment,真正的);
        fragOne.setArguments(参数);
        ft.add(R.id.main_frag_container,fragOne);
        ft.commit();

    }
}
 

有关片段布局...

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:填充=20dp>
    <的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=一些片段/>

    <的LinearLayout
        机器人:方向=垂直
        机器人:ID =@ + ID / frag_container
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>
 

该片段...

 公共类MyFragment扩展片段{
    @覆盖
    公共查看onCreateView(最终LayoutInflater吹气,最后的ViewGroup容器中,最后包savedInstanceState){
        一个ViewGroup布局=(ViewGroup中)inflater.inflate(R.layout.frag_layout,集装箱,假);

        布尔shouldCreateChild = getArguments()getBoolean(shouldYouCreateAChildFragment);

        如果(shouldCreateChild){
            FragmentManager FM = getFragmentManager();
            FragmentTransaction英尺= fm.beginTransaction();

            fm.beginTransaction();
            片段fragTwo =新MyFragment();
            捆绑参数=新包();
            arguments.putBoolean(shouldYouCreateAChildFragment,假);
            fragTwo.setArguments(参数);
            ft.add(R.id.frag_container,fragTwo);
            ft.commit();

        }

        返回布局;
    }
}
 

这个例子包括了那些你需要动态添加片段到尚未被夸大,并加入层次的片段的情况。刚才指定目标片段要通过ID添加到容器中添加一个片段已被夸大,并加入层次的片段很简单,就像你通常会。

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?

I am generating fragments this way:

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

How to add into FRAG4_TAG fragment another one?

Edit2:

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

解决方案

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.

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

The layout for the 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>

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

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

    }
}

The layout for the 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>

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

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天全站免登陆