【问题标题】:Replacing a fragment deletes another fragment替换一个片段会删除另一个片段
【发布时间】: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"/>

【问题讨论】:

    标签: android fragment


    【解决方案1】:

    也许尝试动态替换(左右片段)。不要在您的 xml 布局中定义任何片段。相反,使用 2 FrameLayouts 作为片段的容器,并根据需要仅在代码中动态附加所需的片段。

    【讨论】:

      【解决方案2】:

      我修好了。我不应该尝试替换片段,这会删除它并弄乱主要活动的整个布局。所以我所做的是,在主中创建所有片段,并将它们添加到线性布局内的相对布局中。在 onCreate 方法中,我只是更改了它们的可见性以显示我想要的,并根据微调器的选择隐藏其他的。现在整个应用程序没有任何问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多