【问题标题】:Views value lost on tab change视图值因标签更改而丢失
【发布时间】:2017-12-25 05:24:36
【问题描述】:

我正在开发一个调查应用程序,我面临的问题是布局包含一个问题TextView 是/否RadioGroup 和一个备注EditText 字段。 所以我创建了一个包含这些东西的布局,并根据问题的数量在我的fragment 中包含 5-10 次。问题是通过代码动态设置的。 它是一个包含 4-5 个选项卡/片段的选项卡式活动,现在在选择所需选项并输入备注后,我移至下一个选项卡,但是当我返回同一选项卡时,最后一个视图中的值会复制到该选项卡上可用的所有视图。一旦用户单击下一个按钮在选项卡之间交换被禁用,并且在下一次单击时,所有数据都将保存到模型中(用于在最终选项卡上提交),并且下一个选项卡通过代码 viewPager.setCurrentItem(1); 处于活动状态,选项卡就会更改。模型数据虽然没有回火但是视图数据丢失了

我的自定义布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp">

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:textColor="@color/black"
    android:textStyle="bold" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RadioGroup
        android:id="@+id/rgOptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/rgYes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="@string/yes" />

        <Space
            android:layout_width="20dp"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/rgNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:text="@string/no" />
    </RadioGroup>

</RelativeLayout>

<EditText
    android:id="@+id/etRemarks"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="@string/remarks"
    android:inputType="textMultiLine" />

片段 xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

<Button
        android:id="@+id/bt_odetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next"/>

            <include
                android:id="@+id/view1"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view2"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view3"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view4"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view5"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view6"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view7"
                layout="@layout/observerlayoutcustom1" />

            <include
                android:id="@+id/view8"
                layout="@layout/observerlayoutcustom1" />

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-fragments


    【解决方案1】:

    viewpager 的setOffScreenPageLimit 方法可以解决这个问题。 将 viewpager 的 offScreenpagelimit 设置为 4

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多