如何正确地将多个片段添加到片段转换? [英] How do I properly add multiple fragments to a fragment transition?

查看:179
本文介绍了如何正确地将多个片段添加到片段转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在这里问了一个关于片段的问题:

  http://stackoverflow.com/questions/12443312/listview-不填充或Web视图占用整个屏幕

经过大量的混乱我发现问题所在,但经过更多的调查和研究(其中正确的代码看起来与我的相同),我无法弄清楚我的问题是什么。



在创建完所有内容后,我发现只有添加到事务中的最后一个片段是可见的。这是我的代码来添加它们:

pre $ FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
UrlListFragment urlfragment = new UrlListFragment();
MyWebFragment webfragment = new MyWebFragment();
trans.add(R.id.fragment_container,urlfragment,my_url_fragment);
trans.add(R.id.fragment_container,webfragment,my_web_fragment);
trans.commit();

这是我的主要xml:

 < LinearLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android .com / tools
android:orientation =horizo​​ntal
android:layout_width =fill_parent
android:layout_height =fill_parent>
< FrameLayout
android:id =@ + id / fragment_container
android:layout_width =match_parent
android:layout_height =match_parent>
< / FrameLayout>



我是什么做错了,或者做了什么,所以这两个片段都被正确地添加了,并且可以被正确地看到?

解决方案

你不能以这种方式使用片段,你只需要为每个容器使用一个片段。无论如何,它将达到相同的视觉布局。

 < LinearLayout xmlns:android =http://schemas.android。 com / apk / res / android
xmlns:tools =http://schemas.android.com/tools
android:orientation =horizo​​ntal
android:layout_width =fill_parent
android:layout_height =fill_parent>
< FrameLayout
android:id =@ + id / fragment_container1
android:layout_width =match_parent
android:layout_height =match_parent>
< / FrameLayout>
< FrameLayout
android:id =@ + id / fragment_container2
android:layout_width =match_parent
android:layout_height =match_parent>
< / FrameLayout>
< / LinearLayout>

  FragmentManager manager = getFragmentManager(); 
FragmentTransaction trans = manager.beginTransaction();
UrlListFragment urlfragment = new UrlListFragment();
MyWebFragment webfragment = new MyWebFragment();
trans.add(R.id.fragment_container1,urlfragment,my_url_fragment);
trans.add(R.id.fragment_container2,webfragment,my_web_fragment);
trans.commit();


I recently asked a question about fragments here:

 http://stackoverflow.com/questions/12443312/listview-not-populating-or-webview-taking-up-entire-screen

After a lot of messing around I found what the problem was, but after more fooling around, and research (in which correct code seemed identical to mine), i cant figure out what my problem is.

After everything is created, I find that only the last fragment added to the transaction is visible. This is my code to add them:

    FragmentManager manager = getFragmentManager();
    FragmentTransaction trans = manager.beginTransaction();
    UrlListFragment urlfragment = new UrlListFragment();
    MyWebFragment webfragment = new MyWebFragment();
    trans.add(R.id.fragment_container, urlfragment, "my_url_fragment");
    trans.add(R.id.fragment_container, webfragment, "my_web_fragment");
    trans.commit();

And this is my main xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

What am I doing wrong, or what can be done so both fragments are added correctly and can be seen correctly?

解决方案

Unfortunately you cant use fragments in this way you need to use just one fragment per container. It will amount to the same visual layout anyway.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
    android:id="@+id/fragment_container1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout
    android:id="@+id/fragment_container2"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>

and

FragmentManager manager = getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
UrlListFragment urlfragment = new UrlListFragment();
MyWebFragment webfragment = new MyWebFragment();
trans.add(R.id.fragment_container1, urlfragment, "my_url_fragment");
trans.add(R.id.fragment_container2, webfragment, "my_web_fragment");
trans.commit();

这篇关于如何正确地将多个片段添加到片段转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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