添加相同片段的多个实例 [英] Adding multiple instances of the same fragment
本文介绍了添加相同片段的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在同一片段的多个实例添加到活动。例如:code是
FragmentManager FM = getSupportFragmentManager();
片段片段= fm.findFragmentById(R.id.fragment_content); FragmentTransaction英尺= fm.beginTransaction(); 为(中间体X = 1; X小于5; X = X + 1){
Log.i(断枝,×=+ X);
ft.add(R.id.fragment_content,新SpecimenFragment(),×_+ X);
} ft.commit();
当活动运行仅存在一个添加的片段的实例 - 为什么
有关信息的片段被插入到为活性和一个XML布局
在code引用R.id.fragment_content的定义是:
<的FrameLayout
机器人:ID =@ + ID / fragment_content
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
解决方案
似乎这个问题要使用的FrameLayout
作为碎片的容器。我将其更改为
<的LinearLayout
机器人:ID =@ + ID / fragment_content
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直/>
和现在工作得很好。
I am attempting to add multiple instances of the same fragment to an activity. Example code is
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
FragmentTransaction ft = fm.beginTransaction();
for (int x = 1; x < 5; x = x + 1) {
Log.i("frag","x="+x);
ft.add(R.id.fragment_content, new SpecimenFragment(),"x_"+x);
}
ft.commit();
When the activity runs there is only one instance of the fragment added - why?
For info the fragments are being inserted into an XML layout for the activity and the R.id.fragment_content referenced in the code is defined as :
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
解决方案
The problem seemed to be is using the FrameLayout
as the container for the fragments. I change this to
<LinearLayout
android:id="@+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
and it now works fine.
这篇关于添加相同片段的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文