【发布时间】:2013-06-20 03:25:36
【问题描述】:
我在我的应用程序中遇到以下问题。我想以特定顺序将多个片段添加到垂直 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();
我每次都使用一笔交易,因此我保证它们在屏幕上按该顺序放置。
我的问题是,当方向改变并重新创建 Activity 时,我无法确定它们会以相同的顺序出现在屏幕上。
有人也经历过吗?我该如何解决这个问题?在 LinearLayout 中有两个布局,每个片段都有一个特定的 id 将无济于事,因为我必须添加的片段数量是不确定的(我只是在示例中使用了数字 2)
【问题讨论】:
-
也许setRetainInstance 对你有用
-
你有没有机会解决这个问题?我一直在寻找一段时间没有找到答案。我只需要在运行时动态地将 X 个(可以变化的)片段添加到线性布局中。