【问题标题】:ViewStub reinflates last view inflated on first pass of onCreateViewStub 重新膨胀在 onCreate 的第一次通过时膨胀的最后一个视图
【发布时间】:2011-03-17 15:15:58
【问题描述】:

我的活动有问题。

我的线性布局中有三个视图存根,就像这样 -

<ViewStub 
 android:id="@+id/index_1"
 android:layout="@layout/index_edittext" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
<ViewStub 
 android:id="@+id/index_2"
 android:layout="@layout/index_edittext" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />
<ViewStub 
 android:id="@+id/index_3"
 android:layout="@layout/index_edittext" 
 android:layout_width="fill_parent"
 android:layout_height="wrap_content" />

我的 onCreate 有条件地检查要膨胀的内容:

for (int i = 0; i < 3; i++) {
    int id = convertIndexToId(i); //will turn i into R.id.index_1
    ViewStub stub = findViewById(id);
    if (bShouldBeSpinner) {
        stub.setLayoutResource(R.layout.index_spinner);
        View root = stub.inflate();
        Spinner spin = (Spinner)root.findViewById(R.id.my_spinner);
        spinner.setAdapter(adapter);
        spinner.setSelection(0);
    }
    else {
        stub.setLayoutResource(R.layout.index_edittext);
        View root = stub.inflate();
        EditText et = (EditText)root.findViewById(R.id.my_edittext);
        //et.phoneHome(); just kidding
        et.setText(String.valueOf(System.currentTimeMillis()));
    }
}

我强制 bShouldBeSpinner 为假。编辑文本的输出如下:
1300373517172
1300373517192
1300373517221

但是,当我旋转屏幕并第二次调用 onCreate 时,输出是这样的:
1300373517221
1300373517221
1300373517221

最初这让我认为你应该只膨胀一次视图,并且层次结构保持在 onCreate 之间......但是当我只第一次运行它时,第二次没有为存根显示任何视图。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Spinner style="@style/SearchInput" android:id="@+id/my_spinner" />
</LinearLayout>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <EditText style="@style/SearchInput" android:id="@+id/my_edittext" />
</LinearLayout>

我觉得文档假设了一些我没有注意到或遗漏的东西。有人看到我做错了吗?

编辑

我添加到视图存根 android:inflatedId="index_1_root"... 等

这是最奇怪的事情,当我在 for 循环之后添加这些行时:

EditText v = indexRoot1.findViewById(R.id.index_edit_text);
Log.d(TAG, "EditTExt: " + v);

EditText v2 = indexRoot2.findViewById(R.id.index_edit_text);
Log.d(TAG, "EditTExt: " + v2);

输出显示(我相信)它们是对不同 EditTexts 的引用。

EditTExt:android.widget.EditText@47210fe8
编辑文本:android.widget.EditText@47212ba8

所以它们再次膨胀,但文本设置为第一次通过时设置的最后一个编辑文本。

【问题讨论】:

    标签: android xml view viewstub


    【解决方案1】:

    在使用相同 id 重新创建不同类型的视图时可能会出现一些问题。 ViewStub 被膨胀视图取代。
    我建议使用

    setInflatedId(int inflatedId)
    

    区分膨胀的视图。
    希望有所帮助。

    【讨论】:

    • 我还需要在第二个 onCreate 上给它充气吗?当我第二次不给它们充气时,什么也没有出现。
    • 是的。除非您使用 android:configChanges,否则旋转会再次创建视图。看来我不明白你的问题:)
    • 我更新了问题,第二次夸大错误观点不是问题。不过,我目前还不确定问题是什么,只知道不是什么问题。
    【解决方案2】:

    我没有使用 ViewStubs,而是在这些存根 (android:id="index_roots") 的根目录中添加了一个 id 并使用了

    view.addView( (isSpinner) ? 
        new Spinner(this) : new EditText(this) ); 
    

    为了解决这个问题,我不会马上接受这个答案,我会允许其他人使用我想要的方法来回答。

    【讨论】:

      猜你喜欢
      • 2021-09-03
      • 1970-01-01
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多