2013-01-01 57 views
0

我试图在标题中看到,将相同片段的2个添加到相同的容器中。我正在使用fragmentransaction来添加我的片段。它第一次工作,但是当我尝试添加第二个片段时,片段被替换。向同一个容器中添加2个相同的片段

这里是我的附加码:

public void addNewCategoryFragment(Category c) { 
FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager 
     .beginTransaction(); 
CategoryFragment categoryFragment = new CategoryFragment(c.name); 
fragmentTransaction.add(R.id.fragment_root, categoryFragment, String.valueOf(c.getID())); 
fragmentTransaction.commit(); 
} 

这里有我想要的片段添加到XML:一目了然的Java看起来不错

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/school" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

<LinearLayout 
    android:id="@+id/fragment_root" 
    android:layout_width="match_parent" 
    android:layout_height="413dp" 
    android:orientation="vertical" > 
</LinearLayout> 

<FrameLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<Button 
    android:id="@+id/submit_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:text="Submit" /> 

<Button 
    android:id="@+id/new_category_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|left" 
    android:onClick="createNewCategory" 
    android:text="New Category" /> 
</FrameLayout> 
+0

标签换成“match_parent”中的“FILL_PARENT”值值(在你的情况下''String.valueOf(c.getID())')必须有所不同。是吗? – dmon

+0

是的。 addNewCategoryFragment将一个Category作为参数。我使用android-active-record作为我的ORM,所以getID是sqlite数据库中类别的id。由于它是主键,因此它是唯一的。 –

回答

0

,也可能是布局。不知道你的片段的布局很难说,但我的猜测是它被掩盖了。尝试把一个滚动型身边或设置isScrollContainer为真这样的:

<LinearLayout 
    android:id="@+id/fragment_root" 
    android:layout_width="match_parent" 
    android:layout_height="413dp" 
    android:orientation="vertical" 
    android:isScrollContainer="true"> 
</LinearLayout> 

我会也在布局的休息,因为FILL_PARENT已被弃用

+0

它的工作!谢谢。 –

相关问题