Android之ViewStub的简单使用
1.viewstub就是動態加載試圖;也就是在我們的app啟動繪制頁面的時候,他不會繪制到view樹中;當在代碼中執行inflate操作后,她才會被添加到試圖中。其實ViewStub就是一個寬高都為0的一個View,它默認是不可見的,只有通過調用setVisibility函數或者Inflate函數才 會將其要裝載的目標布局給加載出來,從而達到延遲加載的效果,這個要被加載的布局通過android:layout屬性來設置。最終目的是把app加載頁面的速度提高了,使用戶體驗更好。
2.看一個簡單的demo
viewstub.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns: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"><TextViewandroid: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"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="inflate"android:text="inflate" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="setData"android:text="setdata"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="hide"android:text="hide"/><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="show"android:text="show"/><ViewStubandroid: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_myviewstub);viewStub = findViewById(R.id.vs);//textView = (TextView) findViewById(R.id.hello_tv);空指針,因為viewstub沒有inflate}public void inflate(View view){if (inflate == null) {//inflate只會進行一次,當第二次調用的時候,就會拋異常;也可以try catch進行處理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);//nulltextView = 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.當調用第二次inflate的時候,會報錯:
我們看一下這是為什么?進入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");}}可以看到當viewParent為空或者不是viewgroup時才會報這個錯誤;那么第一次調用的時候,肯定是進去了;發現一個方法replaceSelfWithView(view,parent);view就是我們在布局文件中給viewstub指定的layout所引用的那個布局;parent就是getParent方法得到的,也就是acticity的填充布局LinearLayout;
進去看一下:
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);}}可以發現parent.removeViewInLayout(this);把this就是viewstub從父布局linearlayout中移除了;parent.addView()就是把view(也就是我們引用的布局)添加到了父布局LinearLayout中。
我們用layout inspector來查看一下:
inflate前:可以看到viewstub是灰色的
inflate后:可以看到viewstub直接被移除了,把引用布局直接放到view樹里了。
所以當我們第二次再調用inflate方法時,viewstub的parent已經為空了;就會拋出此異常;
當調用textView = viewStub.findViewById(R.id.hello_tv);//獲取到的textview是空的;
而使用textView = findViewById(R.id.hello_tv);就可以直接拿到控件對象了;
當實現引用布局的顯示和隱藏時,測試發現使用viewstub的setVisibility()方法可以實現,這是為什么呢?;按理說使用constraintLayout.setVisibility()當然也可以;根據上面的view樹結構來看,好像使用引用布局的setVisibility()方法更合理一些;
下面我們再來看看viewstub的setVisibility()為什么也可以;跟進源碼看看:
源碼中使用mInflatedViewRef獲取到view,然后設置隱藏與顯示;mInflatedViewRef是一個view的弱引用WeakReference<View>
其實在上面的inflate方法中已經為其添加了mInflatedViewRef = new WeakReference<>(view);這個view就是viewstub中的引用布局;
所以,使用viewstub可以實現相同的顯示或隱藏效果;
從上圖的最后一個紅色框中可以發現,假設現在我沒有調用inflate方法,而是直接點擊了show按鈕;然后引用布局也可以繪制出來;這就是我在寫demo的時候,直接上去點擊show按鈕,竟然也可以顯示的原因。
?
?
總結
以上是生活随笔為你收集整理的Android之ViewStub的简单使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件测试前景和发展方向
- 下一篇: 转:This Android SDK r