我遇到了我的片段问题,但不知道为什么。我在主要活动中有两个片段。一边(右边)在片段上有一个微调器,在那个片段中,我替换了微调器显示的片段。左侧显示了使用阵列适配器显示数据的列表视图。替换片段删除另一片段
问题是,无论何时我替换左侧的片段,右侧的片段消失,我不知道为什么会这样做。
这是我的微调更换左侧片段代码:
personSelection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
personType = "Student";
StudentFragment studentFrag = StudentFragment.newInstance();
getFragmentManager().beginTransaction()
.replace(R.id.form, studentFrag)
.commit();
} else if (position == 1) {
personType = "Teacher";
TeacherFragment teacherFrag = TeacherFragment.newInstance();
getFragmentManager().beginTransaction()
.replace(R.id.form, teacherFrag)
.commit();
} else if (position == 2) {
personType = "Administrator";
AdminFragment adminFrag = AdminFragment.newInstance();
getFragmentManager().beginTransaction()
.replace(R.id.form, adminFrag)
.commit();
} // end if/else
} // end personSelection
@Override
public void onNothingSelected(AdapterView<?> parent) {}
}); // end selection
右侧片段被用微调器旁边显示新添加的数据到数组列表按钮刷新,这就是只有该片段被使用的时间。
一个视觉展现我的意思。
- 应用程序启动时工作正常:
- 选择微调后:(刷新按钮并不要么在此之后的工作)
这是主要活动在xml:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:layout_height="40dp"
android:id="@+id/person_spinner"
android:entries="@array/child"
android:layout_width="170dp" />
<Button
android:text="Button"
android:id="@+id/refresh_button"
android:drawableStart="@android:drawable/stat_notify_sync"
android:drawableTint="#000000"
android:layout_width="45dp"
android:layout_height="50dp" />
</LinearLayout>
<fragment
android:name="com....StudentFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:id="@+id/form"
tools:layout="@layout/student_fragment" />
</LinearLayout>
<fragment
android:name="com....DetailFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".7"
android:id="@+id/frame_detail"
tools:layout="@layout/fragment_detail"/>