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>
标签换成“match_parent”中的“FILL_PARENT”值值(在你的情况下''String.valueOf(c.getID())')必须有所不同。是吗? – dmon
是的。 addNewCategoryFragment将一个Category作为参数。我使用android-active-record作为我的ORM,所以getID是sqlite数据库中类别的id。由于它是主键,因此它是唯一的。 –