Android?ViewStub使用方法學(xué)習(xí)
前言
當(dāng)渲染一個(gè)活動(dòng)時(shí),這個(gè)活動(dòng)的布局可能會(huì)有很多visible為invisible和gone的情況,雖然這些控件雖然現(xiàn)在不顯示在屏幕上,但是系統(tǒng)在加載這個(gè)布局文件時(shí)還是會(huì)加載它的,這就影響了這個(gè)頁(yè)面的加載效率,因?yàn)檫@些不可見(jiàn)的控件提前加載它們并沒(méi)有什么實(shí)際的意義,反而會(huì)減緩頁(yè)面的加載時(shí)間,所以為了解決這個(gè)問(wèn)題可以使用ViewStub來(lái)懶加載暫時(shí)不顯示的布局.
1.ViewStub的優(yōu)勢(shì)
簡(jiǎn)單來(lái)說(shuō), ViewStub可以做到按需加載一個(gè)布局,我們可以控制它加載的時(shí)機(jī),而不是在Activity的onCreate方法中去加載.即懶加載
2.ViewStub的使用
<ViewStub
android:id="@+id/stub"
android:inflatedId="@+id/text"
android:layout="@layout/text_view_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/textView3"
android:layout_marginTop="180dp"
android:layout_marginLeft="100dp"/>屬性 功能
android:inflatedId="@+id/text" 為我們要加載的布局提供一個(gè)id android:layout 我們需要加載的布局 除此之外
app:layout_constraintTop_toBottomOf="@id/textView3"
android:layout_marginTop="180dp"
android:layout_marginLeft="100dp"/>這些代表我們懶加載的布局在父布局的位置,如果懶加載的布局有相同的屬性,將會(huì)被覆蓋
//通過(guò)id得到viewStub對(duì)象 ViewStub viewStub = findViewById(R.id.stub); //動(dòng)態(tài)加載布局 viewStub.inflate();
簡(jiǎn)單實(shí)戰(zhàn)
1.viewstub就是動(dòng)態(tài)加載試圖
也就是在我們的app啟動(dòng)繪制頁(yè)面的時(shí)候,他不會(huì)繪制到view樹中;當(dāng)在代碼中執(zhí)行inflate操作后,她才會(huì)被添加到試圖中。其實(shí)ViewStub就是一個(gè)寬高都為0的一個(gè)View,它默認(rèn)是不可見(jiàn)的,只有通過(guò)調(diào)用setVisibility函數(shù)或者Inflate函數(shù)才 會(huì)將其要裝載的目標(biāo)布局給加載出來(lái),從而達(dá)到延遲加載的效果,這個(gè)要被加載的布局通過(guò)android:layout屬性來(lái)設(shè)置。最終目的是把a(bǔ)pp加載頁(yè)面的速度提高了,使用戶體驗(yàn)更好。
2.看一個(gè)簡(jiǎn)單的demo
viewstub.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/inflatedStart"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/hello_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="DATA EMPTY!"/>
</android.support.constraint.ConstraintLayout>
activity_myviewstub.xml
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="inflate"
android:text="inflate" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="setData"
android:text="setdata"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="hide"
android:text="hide"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="show"
android:text="show"/>
<ViewStub
android:id="@+id/vs"
android:inflatedId="@+id/inflatedStart"
android:layout="@layout/viewstub"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyViewStubActivity.java
package com.ysl.myandroidbase.viewstub;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewStub;
import android.widget.TextView;
import com.ysl.myandroidbase.R;
public class MyViewStubActivity extends AppCompatActivity {
private ViewStub viewStub;
private TextView textView;
private View inflate;
private ConstraintLayout constraintLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_myviewstub);
viewStub = findViewById(R.id.vs);
//textView = (TextView) findViewById(R.id.hello_tv);空指針,因?yàn)関iewstub沒(méi)有inflate
}
public void inflate(View view){
if (inflate == null) {//inflate只會(huì)進(jìn)行一次,當(dāng)?shù)诙握{(diào)用的時(shí)候,就會(huì)拋異常;也可以try catch進(jìn)行處理
inflate = viewStub.inflate();
constraintLayout = findViewById(R.id.inflatedStart);
System.out.println(constraintLayout);
System.out.println("viewStub-------->"+viewStub);
textView = viewStub.findViewById(R.id.hello_tv);//獲取到的textview是空的;
System.out.println("viewStub textView-------->"+textView);//null
textView = constraintLayout.findViewById(R.id.hello_tv);
System.out.println("constraintLayout textView-------->"+textView);
textView = findViewById(R.id.hello_tv);
System.out.println("textView-------->"+textView);
}
}
public void setData(View view){
if (constraintLayout != null) {
textView = constraintLayout.findViewById(R.id.hello_tv);
textView.setText("HAVE DATA !!!");
}
}
public void hide(View view){
viewStub.setVisibility(View.GONE);
// if (constraintLayout != null){
// constraintLayout.setVisibility(View.GONE);
// }
}
public void show(View view){
viewStub.setVisibility(View.VISIBLE);
// if (constraintLayout != null){
// constraintLayout.setVisibility(View.VISIBLE);
// }
}
}
3.當(dāng)調(diào)用第二次inflate的時(shí)候,會(huì)報(bào)錯(cuò):

編輯切換為居中
添加圖片注釋,不超過(guò) 140 字(可選)
我們看一下這是為什么?進(jìn)入viewStub.inflate();的源碼:
public View inflate() {
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
if (mLayoutResource != 0) {
final ViewGroup parent = (ViewGroup) viewParent;
final View view = inflateViewNoAdd(parent);
replaceSelfWithView(view, parent);
mInflatedViewRef = new WeakReference<>(view);
if (mInflateListener != null) {
mInflateListener.onInflate(this, view);
}
return view;
} else {
throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
}
} else {
throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
}
}
可以看到當(dāng)viewParent為空或者不是viewgroup時(shí)才會(huì)報(bào)這個(gè)錯(cuò)誤;那么第一次調(diào)用的時(shí)候,肯定是進(jìn)去了;發(fā)現(xiàn)一個(gè)方法replaceSelfWithView(view,parent);view就是我們?cè)诓季治募薪oviewstub指定的layout所引用的那個(gè)布局;parent就是getParent方法得到的,也就是acticity的填充布局LinearLayout;
進(jìn)去看一下:
private void replaceSelfWithView(View view, ViewGroup parent) {
final int index = parent.indexOfChild(this);
parent.removeViewInLayout(this);
final ViewGroup.LayoutParams layoutParams = getLayoutParams();
if (layoutParams != null) {
parent.addView(view, index, layoutParams);
} else {
parent.addView(view, index);
}
}
可以發(fā)現(xiàn)parent.removeViewInLayout(this);把this就是viewstub從父布局linearlayout中移除了;parent.addView()就是把view(也就是我們引用的布局)添加到了父布局LinearLayout中。
我們用layout inspector來(lái)查看一下:
inflate前:可以看到viewstub是灰色的

編輯
添加圖片注釋,不超過(guò) 140 字(可選)
inflate后:可以看到viewstub直接被移除了,把引用布局直接放到view樹里了。

編輯
添加圖片注釋,不超過(guò) 140 字(可選)
所以當(dāng)我們第二次再調(diào)用inflate方法時(shí),viewstub的parent已經(jīng)為空了;就會(huì)拋出此異常;
當(dāng)調(diào)用textView = viewStub.findViewById(R.id.hello_tv);//獲取到的textview是空的;
而使用textView = findViewById(R.id.hello_tv);就可以直接拿到控件對(duì)象了;
當(dāng)實(shí)現(xiàn)引用布局的顯示和隱藏時(shí),測(cè)試發(fā)現(xiàn)使用viewstub的setVisibility()方法可以實(shí)現(xiàn),這是為什么呢?;按理說(shuō)使用constraintLayout.setVisibility()當(dāng)然也可以;根據(jù)上面的view樹結(jié)構(gòu)來(lái)看,好像使用引用布局的setVisibility()方法更合理一些;
下面我們?cè)賮?lái)看看viewstub的setVisibility()為什么也可以;跟進(jìn)源碼看看:

編輯切換為居中
添加圖片注釋,不超過(guò) 140 字(可選)
源碼中使用mInflatedViewRef獲取到view,然后設(shè)置隱藏與顯示;mInflatedViewRef是一個(gè)view的弱引用WeakReference
其實(shí)在上面的inflate方法中已經(jīng)為其添加了mInflatedViewRef = new WeakReference<>(view);這個(gè)view就是viewstub中的引用布局;
所以,使用viewstub可以實(shí)現(xiàn)相同的顯示或隱藏效果;
從上圖的最后一個(gè)紅色框中可以發(fā)現(xiàn),假設(shè)現(xiàn)在我沒(méi)有調(diào)用inflate方法,而是直接點(diǎn)擊了show按鈕;然后引用布局也可以繪制出來(lái);這就是我在寫demo的時(shí)候,直接上去點(diǎn)擊show按鈕,竟然也可以顯示的原因。

編輯切換為居中
添加圖片注釋,不超過(guò) 140 字(可選)
以上就是Android ViewStub的使用與簡(jiǎn)單的演練;如果想要進(jìn)階自己Android技能,可以參考這份《Android核心技術(shù)筆記》里面記錄有Android的核心技術(shù)與其他前沿技術(shù)。
文末
Android ViewStub的使用注意事項(xiàng)
inflate方法只能調(diào)用一次,再次調(diào)用會(huì)出異常 我們看下inflate方法的源碼,一旦第二次調(diào)用inflate方法,我們的到viewParent將等于null,會(huì)報(bào) throw new IllegalStateException(“ViewStub must have a non-null ViewGroup viewParent”);異常,所以總之一句話,這個(gè)懶加載只能加載一次
public View inflate() {
final ViewParent viewParent = getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
if (mLayoutResource != 0) {
final ViewGroup parent = (ViewGroup) viewParent;
final View view = inflateViewNoAdd(parent);
replaceSelfWithView(view, parent);
mInflatedViewRef = new WeakReference<>(view);
if (mInflateListener != null) {
mInflateListener.onInflate(this, view);
}
return view;
} else {
throw new IllegalArgumentException("ViewStub must have a valid layoutResource");
}
} else {
throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent");
}
}以上就是Android ViewStub使用方法學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于Android ViewStub使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
gradle配置國(guó)內(nèi)鏡像的實(shí)現(xiàn)
這篇文章主要介紹了gradle配置國(guó)內(nèi)鏡像的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Android 8.0不能自動(dòng)安裝APK問(wèn)題的解決方法(完美適配)
這篇文章主要給大家介紹了關(guān)于Android 8.0不能自動(dòng)安裝APK問(wèn)題的解決方法(完美適配),這里的自動(dòng)安裝是指下載完成后,自動(dòng)彈出安裝界面,而不是靜默安裝APK,文中介紹的非常詳細(xì),需要的朋友可以參考下2018-07-07
Android短信操作常見(jiàn)協(xié)議和常用代碼
這篇文章主要介紹了Android短信操作常見(jiàn)協(xié)議和常用代碼,本文直接給出代碼實(shí)例,需要的朋友可以參考下2015-04-04

