简体   繁体   English

切换已经膨胀的 stubview 布局的正确方法是什么?

[英]what is proper way to switch stubview layout that has been inflated already?

I have a simple relative layout included two stubview looks like below:我有一个简单的相对布局,包括两个 stubview,如下所示:

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

    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_login"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout="@layout/layout_main_login"/>

    <ViewStub
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/layout_member"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout="@layout/layout_main_member"/>

</RelativeLayout>

after two stubviews has been inflated, what is the proper way to switch inflated layout visible or gone agagin?在两个 stubviews 被充气后,切换充气布局可见或再次消失的正确方法是什么? I am a little confused, since the stubview removed from parent view hierarchy, the stubview will be null at that point.我有点困惑,因为 stubview 从父视图层次结构中删除,此时 stubview 将为空。

I have took a look at https://developer.android.com/training/improving-layouts/loading-ondemand.html and http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html我查看了https://developer.android.com/training/improving-layouts/loading-ondemand.htmlhttp://android-developers.blogspot.com/2009/03/android-layout-tricks- 3-optimize-with.html

am I misunderstand some points, that stubview once inflated will not be able to set visible or gone again?我是否误解了一些观点,一旦膨胀后的 stubview 将无法设置为可见或再次消失?

my walkaround is wrap the stubview by famelayout or other layout, and change the visiblity of that layout.我的解决方法是通过成名布局或其他布局包装 stubview,并更改该布局的可见性。

In the similar situation I use the following code:在类似的情况下,我使用以下代码:

View layoutInflated;

void setStubViewVisibility(final boolean visible) {
    if (null == layoutInflated) {
        if (visible) {
            layoutInflated= ((ViewStub) view.findViewById(R.id.layout_login)).inflate();
            layoutInflated.setVisibility(View.VISIBLE);
        }
    } else {
        layoutInflated.setVisibility(visible ? View.VISIBLE : View.GONE);
    }
}

So you don't have to place ViewStub into another layout.因此您不必将ViewStub放入另一个布局中。 Just check if layout has been inflated.只需检查布局是否已膨胀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM