最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android?ViewStub使用方法學(xué)習(xí)

 更新時間:2022年11月13日 09:47:50   作者:Coolbreeze  
這篇文章主要為大家介紹了Android?ViewStub使用方法學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

前言

當(dāng)渲染一個活動時,這個活動的布局可能會有很多visible為invisible和gone的情況,雖然這些控件雖然現(xiàn)在不顯示在屏幕上,但是系統(tǒng)在加載這個布局文件時還是會加載它的,這就影響了這個頁面的加載效率,因為這些不可見的控件提前加載它們并沒有什么實際的意義,反而會減緩頁面的加載時間,所以為了解決這個問題可以使用ViewStub來懶加載暫時不顯示的布局.

1.ViewStub的優(yōu)勢

簡單來說, ViewStub可以做到按需加載一個布局,我們可以控制它加載的時機(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" 為我們要加載的布局提供一個id android:layout 我們需要加載的布局 除此之外

        app:layout_constraintTop_toBottomOf="@id/textView3"
        android:layout_marginTop="180dp"
        android:layout_marginLeft="100dp"/>

這些代表我們懶加載的布局在父布局的位置,如果懶加載的布局有相同的屬性,將會被覆蓋

//通過id得到viewStub對象
ViewStub viewStub = findViewById(R.id.stub);
//動態(tài)加載布局
 viewStub.inflate();

簡單實戰(zhàn)

1.viewstub就是動態(tài)加載試圖

也就是在我們的app啟動繪制頁面的時候,他不會繪制到view樹中;當(dāng)在代碼中執(zhí)行inflate操作后,她才會被添加到試圖中。其實ViewStub就是一個寬高都為0的一個View,它默認(rèn)是不可見的,只有通過調(diào)用setVisibility函數(shù)或者Inflate函數(shù)才 會將其要裝載的目標(biāo)布局給加載出來,從而達(dá)到延遲加載的效果,這個要被加載的布局通過android:layout屬性來設(shè)置。最終目的是把a(bǔ)pp加載頁面的速度提高了,使用戶體驗更好。

2.看一個簡單的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);空指針,因為viewstub沒有inflate
    }
    public  void inflate(View view){
        if (inflate == null) {//inflate只會進(jìn)行一次,當(dāng)?shù)诙握{(diào)用的時候,就會拋異常;也可以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的時候,會報錯:

編輯切換為居中

添加圖片注釋,不超過 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時才會報這個錯誤;那么第一次調(diào)用的時候,肯定是進(jìn)去了;發(fā)現(xiàn)一個方法replaceSelfWithView(view,parent);view就是我們在布局文件中給viewstub指定的layout所引用的那個布局;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來查看一下:

inflate前:可以看到viewstub是灰色的

編輯

添加圖片注釋,不超過 140 字(可選)

inflate后:可以看到viewstub直接被移除了,把引用布局直接放到view樹里了。

編輯

添加圖片注釋,不超過 140 字(可選)

所以當(dāng)我們第二次再調(diào)用inflate方法時,viewstub的parent已經(jīng)為空了;就會拋出此異常;

當(dāng)調(diào)用textView = viewStub.findViewById(R.id.hello_tv);//獲取到的textview是空的;

而使用textView = findViewById(R.id.hello_tv);就可以直接拿到控件對象了;

當(dāng)實現(xiàn)引用布局的顯示和隱藏時,測試發(fā)現(xiàn)使用viewstub的setVisibility()方法可以實現(xiàn),這是為什么呢?;按理說使用constraintLayout.setVisibility()當(dāng)然也可以;根據(jù)上面的view樹結(jié)構(gòu)來看,好像使用引用布局的setVisibility()方法更合理一些;

下面我們再來看看viewstub的setVisibility()為什么也可以;跟進(jìn)源碼看看:

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

源碼中使用mInflatedViewRef獲取到view,然后設(shè)置隱藏與顯示;mInflatedViewRef是一個view的弱引用WeakReference

其實在上面的inflate方法中已經(jīng)為其添加了mInflatedViewRef = new WeakReference<>(view);這個view就是viewstub中的引用布局;

所以,使用viewstub可以實現(xiàn)相同的顯示或隱藏效果;

從上圖的最后一個紅色框中可以發(fā)現(xiàn),假設(shè)現(xiàn)在我沒有調(diào)用inflate方法,而是直接點擊了show按鈕;然后引用布局也可以繪制出來;這就是我在寫demo的時候,直接上去點擊show按鈕,竟然也可以顯示的原因。

編輯切換為居中

添加圖片注釋,不超過 140 字(可選)

以上就是Android ViewStub的使用與簡單的演練;如果想要進(jìn)階自己Android技能,可以參考這份《Android核心技術(shù)筆記》里面記錄有Android的核心技術(shù)與其他前沿技術(shù)。

文末

Android ViewStub的使用注意事項

inflate方法只能調(diào)用一次,再次調(diào)用會出異常 我們看下inflate方法的源碼,一旦第二次調(diào)用inflate方法,我們的到viewParent將等于null,會報 throw new IllegalStateException(“ViewStub must have a non-null ViewGroup viewParent”);異常,所以總之一句話,這個懶加載只能加載一次

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使用的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Android入門教程之ListView的具體使用詳解

    Android入門教程之ListView的具體使用詳解

    列表作為最常用的控件之一,還是有必要好好學(xué)習(xí)的,本章以一個初學(xué)者的角度來學(xué)習(xí) ListView,ListView的屬性,以及BaseAdapter簡單定義,至于ListView優(yōu)化這些, 我們一步步來
    2021-10-10
  • 詳解Android啟動第一幀

    詳解Android啟動第一幀

    這篇文章我們就來介紹Android啟動第一幀,至于Android第一幀什么時候開始調(diào)度,具體內(nèi)容我們就來看下面文章內(nèi)容吧,感興趣得小伙伴可以和小編一起來學(xué)習(xí)奧
    2021-10-10
  • Android仿支付寶微信支付密碼界面彈窗封裝dialog

    Android仿支付寶微信支付密碼界面彈窗封裝dialog

    這篇文章主要介紹了Android仿支付寶微信支付密碼界面彈窗封裝dialog的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-03-03
  • Android使用ViewPager實現(xiàn)屏幕滑動效果

    Android使用ViewPager實現(xiàn)屏幕滑動效果

    這篇文章主要為大家詳細(xì)介紹了Android使用ViewPager實現(xiàn)屏幕滑動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 詳解Android aidl的使用方法

    詳解Android aidl的使用方法

    AIDL是Android中IPC(Inter-Process Communication)方式中的一種,AIDL是Android Interface definition language的縮寫。這篇文章主要介紹了Android aidl的使用方法,感興趣的朋友跟隨小編一起看看吧
    2020-07-07
  • gradle配置國內(nèi)鏡像的實現(xiàn)

    gradle配置國內(nèi)鏡像的實現(xiàn)

    這篇文章主要介紹了gradle配置國內(nèi)鏡像的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Android 8.0不能自動安裝APK問題的解決方法(完美適配)

    Android 8.0不能自動安裝APK問題的解決方法(完美適配)

    這篇文章主要給大家介紹了關(guān)于Android 8.0不能自動安裝APK問題的解決方法(完美適配),這里的自動安裝是指下載完成后,自動彈出安裝界面,而不是靜默安裝APK,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07
  • Android短信操作常見協(xié)議和常用代碼

    Android短信操作常見協(xié)議和常用代碼

    這篇文章主要介紹了Android短信操作常見協(xié)議和常用代碼,本文直接給出代碼實例,需要的朋友可以參考下
    2015-04-04
  • 基于Android實現(xiàn)煙花效果

    基于Android實現(xiàn)煙花效果

    這篇文章文章我們將介紹如何通過Canvas 2D坐標(biāo)系實現(xiàn)粒子效果,文中通過代碼示例和圖文結(jié)合介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動手嘗試一下
    2023-11-11
  • Android Flutter表格組件Table的使用詳解

    Android Flutter表格組件Table的使用詳解

    Table組件不同于其它Flex布局,它是直接繼承的RenderObjectWidget的。本篇文章主要介紹如何在頁面中使用表格做一個記錄,感興趣的可以嘗試一下
    2022-06-06

最新評論

建水县| 天门市| 鱼台县| 大庆市| 宽甸| 当阳市| 米脂县| 来安县| 南澳县| 思茅市| 邯郸市| 金华市| 招远市| 冷水江市| 洛扎县| 新河县| 辽中县| 游戏| 呼伦贝尔市| 舟曲县| 卓资县| 建平县| 乳源| 阿拉尔市| 渝中区| 瓮安县| 贵阳市| 贺州市| 霍林郭勒市| 鄂托克旗| 宽甸| 内黄县| 塘沽区| 南溪县| 郑州市| 五家渠市| 沅江市| 肥乡县| 贵港市| 凉城县| 连州市|