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

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

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

前言

當(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入門教程之ListView的具體使用詳解

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

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

    詳解Android啟動(dòng)第一幀

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

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

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

    Android使用ViewPager實(shí)現(xiàn)屏幕滑動(dòng)效果

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

    詳解Android aidl的使用方法

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

    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)題的解決方法(完美適配)

    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é)議和常用代碼

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

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

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

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

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

最新評(píng)論

巴林右旗| 江西省| 乐安县| 固始县| 惠安县| 桂东县| 青海省| 涞源县| 浮梁县| 苍梧县| 巧家县| 阿荣旗| 扬州市| 都昌县| 纳雍县| 安龙县| 达孜县| 丰镇市| 白城市| 枣强县| 寻乌县| 金门县| 永胜县| 尼勒克县| 石家庄市| 东辽县| 拉萨市| 靖江市| 磐石市| 冀州市| 吉木萨尔县| 泰兴市| 鹤岗市| 巩留县| 长垣县| 四川省| 江门市| 扶余县| 临夏县| 龙胜| 城口县|