ViewStub 重新膨胀在 onCreate 第一次传递时膨胀的最后一个视图

发布于 2024-10-22 20:21:32 字数 2808 浏览 6 评论 0原文

我的活动有问题。

我的线性布局中有三个视图存根,如下所示 -

<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 为 false。编辑文本的输出如下:
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);

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

EditTExt:android.widget.EditText@47210fe8
EditTExt:android.widget.EditText@47212ba8

所以它们再次膨胀,但文本设置为最后一次编辑文本在第一次传递时设置的内容。

I have a bug with my activity.

I have three view stubs in my linear layout like so -

<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" />

my onCreate conditionally checks what to inflate:

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()));
    }
}

I force bShouldBeSpinner to false. The output of the edittext's is as follows:
1300373517172
1300373517192
1300373517221

However, when I rotate the screen and onCreate is called a second time the output is this:
1300373517221
1300373517221
1300373517221

Initially that made me think you should only inflate the view once, and the heirarchy is kept inbetween onCreate's... however when i only run it the first time the second time no views are shown for the stubs.

<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>

I feel the documentation is assuming something that I did not notice or am missing. Does anyone see what I am doing wrong?

EDIT

I added to the view stubs android:inflatedId="index_1_root"... etc

it is the strangest thing, when I add these lines after the for loop:

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);

the output says (I believe) they are references to different EditTexts.

EditTExt: android.widget.EditText@47210fe8
EditTExt: android.widget.EditText@47212ba8

So they are getting inflated again, but the text is set to what the last edittext was set to on the first pass.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伤感在游骋 2024-10-29 20:21:32

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

setInflatedId(int inflatedId)

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

There may be some issues when recreating views of different types with the same id.
ViewStub is replaced by inflated view.
I suggest using

setInflatedId(int inflatedId)

to distinguish inflated views.
Hope that help.

稚气少女 2024-10-29 20:21:32

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

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

解决这个问题,但是我不会立即接受这个答案,我将允许其他人使用我想要的方法。

Instead of using ViewStubs, I added an id to the root of those stubs (android:id="index_roots") and used

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

to fix this problem, I will however not accept this answer right away, I'll allow others to answer using the method I was going for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文