【发布时间】:2017-05-09 06:33:42
【问题描述】:
我的片段有问题,但不知道为什么。我有两个关于主要活动的片段。一侧(右)在片段上方有一个微调器,在那个片段中,我用微调器替换了显示的片段。左侧显示了一个列表视图,其中使用数组适配器显示了数据。
问题是,每当我替换左侧的片段时,右侧的片段就会消失,我不知道为什么会这样。
这是我的微调器替换左侧片段的代码:
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"/>
【问题讨论】: