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