Android ViewStub的使用

3,916 阅读2分钟

介绍一下ViewStub的使用吧。

1、ViewStub介绍

ViewStup是一个轻量级的view,之所以说它是轻量级的view是因为它在页面加载渲染的过程中不会去绘制,只是在你需要的时候再去绘制。

ViewStub标签的作用是用于懒加载布局,当系统碰到ViewStub标签的时候是不进行绘制处理(如measure、layout等),比设置View隐藏、不可见更高效。

2、原因

当真正需要显示某一个布局的时候才去渲染。当ViewStub变得可见或 inflate() 的时候,布局才会被加载(替换 ViewStub)。

3、好处

ViewStub 标签懒加载有以下几个好处:

  • 可以避免ViewStub所对应的视图 measure、layout 等性能消耗,只有在使用时才会执行,例如在 APP 启动时,使用它可以提升启动速度。
  • 加载的视图是层叠的,设置不当,会造成过度绘制等问题。而使用 ViewStub 懒加载,则不会执行渲染。
  • 避免了视图树中,调用 requestLayout 时的性能消耗。
  • 避免了 View 创建、初始化等资源消耗,因为 ViewStub 是一个轻量级的组件,占用很少的资源。

4、对比invisible、gone用法

  • 如果我们不使用ViewStub,而是设置View为invisible时,View在layout布局文件中会占用位置,但是View的状态是不可见的,该View还是会创建对象、会被初始化、会占用资源。
  • 如果设置View为gone,View在layout布局文件中不占用位置,但是还是会创建对象、会被初始化、会占用资源。

5、优缺点

  • 优点:占用内存少,页面渲染速度快,避免资源浪费
  • 缺点:不能使用</merge>标签

6、举个栗子

viewStup在使用上类似于include标签,只是在引用layout的时候使用,下面我们来看一个简单的实例来加深理解:

public class MainActivity extends AppCompatActivity {
    private Button btn;
    private ViewStub viewStub;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        btn = (Button) findViewById(R.id.btn);
        viewStub = (ViewStub) findViewById(R.id.view_stub);
 
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewStub.inflate();
            }
        });
    }
}

这是activity_main的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点我"/>
 
    <ViewStub
        android:id="@+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout="@layout/viewstub_view"/>
 
</LinearLayout>

这是viewstub_view的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="需要我时我才显示,么么哒!!" />
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="需要我时我才显示,么么哒!!" />
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="需要我时我才显示,么么哒!!" />
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="需要我时我才显示,么么哒!!" />
</LinearLayout>

运行程序,页面上只有一个button显示,

1.jpeg

点击按钮后,执行inflate();方法后

2.png

看到这不要关掉页面,继续往下看!!!!

注意:

1.viewStub不能使用merge标签(merge标签用法请看juejin.cn/post/709047…

2.viewStub在不能连续调用inflate();会报错,因为一旦inflate()后,viewStub的位置就被占用了,再次调用就会报错。