ViewStub 簡單使用

一、前言

ViewStub 是布局優(yōu)化的方式之一,適用于一些延遲加載的場景,相對于設(shè)置 View.GONE<br />的優(yōu)點(diǎn)是邏輯簡單控制靈活,但是缺點(diǎn)也很明顯,更耗資源,不管可見不可見都會被創(chuàng)建。ViewStub更加輕量級,它本身是一個不可見不占用位置的 View,資源消耗比較小,只有調(diào)用了ViewStub.inflate()的時候加載布局,布局才會實(shí)例化。
<a name="6QFnm"></a>

二、使用

先看效果圖:<br />
iShot2020-07-1316.56.08.gif
  • 布局文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/tv_viewstub_title"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="viewstub 的簡單使用"
        android:textSize="17sp"
        android:textColor="@color/white"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        android:background="@color/black_999999"/>

    <Button
        android:id="@+id/btn_viewstub_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示"
        app:layout_constraintTop_toBottomOf="@+id/tv_viewstub_title"
        app:layout_constraintLeft_toLeftOf="parent"/>
    <Button
        android:id="@+id/btn_viewstub_hide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="隱藏"
        app:layout_constraintTop_toBottomOf="@+id/tv_viewstub_title"
        app:layout_constraintLeft_toRightOf="@+id/btn_viewstub_show"/>

    <Button
        android:id="@+id/btn_viewstub_midify"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改內(nèi)容"
        app:layout_constraintTop_toBottomOf="@+id/tv_viewstub_title"
        app:layout_constraintLeft_toRightOf="@+id/btn_viewstub_hide"/>


    <ViewStub
        android:id="@+id/vs_viewstub_sv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="100dp"
        app:layout_constraintTop_toBottomOf="@+id/tv_viewstub_title"
        android:layout="@layout/include_layout"/>



</androidx.constraintlayout.widget.ConstraintLayout>
  • 延遲加載的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/rl_viewstub_outer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/colorPrimary"
   >


    <TextView
        android:id="@+id/tv_include_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="可以改變的內(nèi)容"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:layout_centerInParent="true"/>

</RelativeLayout>
  • activity
class ViewStubActivity: AppCompatActivity() {
     var  inflate: View? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_viewstub)

        btn_viewstub_show.setOnClickListener {
            //inflate 只能調(diào)用一次
            if (inflate == null){
                 inflate = vs_viewstub_sv.inflate()
            }
            //調(diào)用這個方法會閃退,viewstub 加載過后就會被移除
            //vs_viewstub_sv.visibility = View.VISIBLE
            rl_viewstub_outer.visibility = View.VISIBLE
        }
        btn_viewstub_hide.setOnClickListener {
            //vs_viewstub_sv.visibility = View.GONE
            rl_viewstub_outer.visibility = View.GONE
        }
        btn_viewstub_midify.setOnClickListener {
            //在 viewStub inflate 之前不可調(diào)用
            // tv_include_layout.text = "任意改變的內(nèi)容"
            if (inflate == null){
                inflate = vs_viewstub_sv.inflate()
                tv_include_layout.text = "修改過后的內(nèi)容"
            }else{
                tv_include_layout.text = "修改過后的內(nèi)容"
            }
        }
    }
}

<a name="i7Crk"></a>

三、注意事項(xiàng)

    1. ViewStub只能Inflate一次,之后ViewStub對象會被置為空。
    1. ViewStub只能用來Inflate一個布局文件,而不是某個具體的View
    1. 想要控制顯示與隱藏的是一個布局文件,而非某個View。
    1. 某些布局屬性要加在ViewStub而不是實(shí)際的布局上面,才會起作用,比如上面用的android:layout_margin*系列屬性,如果加在TextView上面,則不會起作用,需要放在它的ViewStub上面才會起作用。而ViewStub的屬性在inflate()后會都傳給相應(yīng)的布局。

<br />
<a name="Y0Au1"></a>

參考

ViewStub用法

Android進(jìn)階——布局優(yōu)化之靈活借助ViewStub實(shí)現(xiàn)懶加載

Android UI布局優(yōu)化之ViewStub

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容