5
我在我的應用程序中遇到以下問題。我想以特定順序將多個片段添加到垂直LinearLayout中。垂直Linearlayout中的多個片段
這裏是我的佈局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
這裏是我用添加片段代碼。
Fragment fragment1 = MyFragment.newInstance(param1);
Fragment fragment2 = MyFragment.newInstance(param2);
FragmentManager fm = getSupportFragmentmanager();
fm.beginTransaction().add(R.id.content, fragment1, "fragment1").commit();
fm.beginTransaction().add(R.id.content, fragment2, "fragment2").commit();
我每次使用一次事務,所以我保證它們按順序放置在屏幕上。
我的問題是,當方向改變並且活動被重新創建時,我不能確定它們將以相同順序出現在屏幕上。
有人也遇到過這個嗎?我該如何解決這個問題?在LinearLayout中有兩個佈局,並且每個片段都有一個特定的ID將不會有幫助,因爲我必須添加的片段數量未確定(我剛剛使用數字2作爲示例)
也許[setRetainInstance(HTTP:// developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean))對你很有用 – zozelfelfo
你解決了這個問題的任何機會?我一直在尋找一段時間,但沒有找到答案。我只需要在運行時動態地將X數(可以變化)的碎片添加到線性佈局。 –