日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中使用ViewStub提高布局性能

發布時間:2025/7/14 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中使用ViewStub提高布局性能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

注,關于Stub的解釋:在Java中,樁(stub)是指用來代替關聯代碼或者未實現代碼的代碼.

ViewStub使用場景

如上圖所示,

  • 一個ListView包含了諸如新聞,商業,科技等Item
  • 每個Item又包含了各自對應的子話題,
  • 但是子話題的View(藍色區域)只有在點擊展開按鈕才真正需要加載.
  • 如果默認加載子話題的View,則會造成內存的占用和CPU的消耗

所以,這時候就ViewStub就派上用處了.使用ViewStub可以延遲加載布局資源.

ViewStub 怎么用

  • 在布局文件中使用ViewStub標簽
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:paddingBottom="@dimen/activity_vertical_margin"tools:context="com.droidyue.viewstubsample.MainActivity"><Buttonandroid:id="@+id/clickMe"android:text="Hello World!"android:layout_width="wrap_content"android:layout_height="wrap_content"/><ViewStubandroid:id="@+id/myViewStub"android:inflatedId="@+id/myInflatedViewId"android:layout="@layout/include_merge"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/clickMe"/> </RelativeLayout>

    2.在代碼中inflate布局

    1 2 3 4 5 6 ViewStub myViewStub = (ViewStub)findViewById(R.id.myViewStub); if (myViewStub != null) {myViewStub.inflate();//或者是下面的形式加載//myViewStub.setVisibility(View.VISIBLE); }

    關于ViewStub的事

    • 除了inflate方法外,我們還可以調用setVisibility()方法加載布局文件
    • 一旦加載布局完成后,ViewStub會從當前布局層級中刪除
    • android:id指定ViewStub ID,用于查找ViewStub進行延遲加載
    • android:layout延遲加載布局的資源id
    • android:inflatedId加載的布局被重寫的id,這里為RelativeLayout的id

    ViewStub的不足

    官方的文檔中有這樣一段描述

    Note: One drawback of ViewStub is that it doesn’t currently support the?tag in the layouts to be inflated.

    意思是ViewStub不支持<merge>標簽.

    關于不支持<merge>標簽的程度,我們進行一個簡單的驗證

    驗證一:直接標簽

    如下,我們有布局文件名為merge_layout.xml

    1 2 3 4 5 6 7 8 9 10 11 12 13 <merge xmlns:android="http://schemas.android.com/apk/res/android"><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="Yes"/><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="No"/></merge>

    替換對應的ViewStub的android:layout屬性值之后,運行后(點擊Button按鈕)得到產生了如下的崩潰

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 E AndroidRuntime: android.view.InflateException: Binary XML file line #1: <merge /> can be used only with a valid ViewGroup root and attachToRoot=trueE AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:551)E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:429)E AndroidRuntime: at android.view.ViewStub.inflate(ViewStub.java:259)E AndroidRuntime: at com.droidyue.viewstubsample.MainActivity$1.onClick(MainActivity.java:20)E AndroidRuntime: at android.view.View.performClick(View.java:5697)E AndroidRuntime: at android.widget.TextView.performClick(TextView.java:10815)E AndroidRuntime: at android.view.View$PerformClick.run(View.java:22526)E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)E AndroidRuntime: at android.os.Looper.loop(Looper.java:158)E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7237)E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)E AndroidRuntime: Caused by: android.view.InflateException: <merge /> can be used only with a valid ViewGroup root and attachToRoot=trueE AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:491)E AndroidRuntime: ... 13 more

    可見,直接的<merge>標簽,ViewStub是不支持的.

    驗證二 間接的ViewStub

    下面布局間接使用了merge標簽.文件名為include_merge.xml

    1 2 3 4 5 6 7 <?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"><include layout="@layout/merge_layout"/> </LinearLayout>

    然后修改ViewStub的android:layout值,運行,一切正常.

    除此之外,本例也驗證了ViewStub也是對<include>標簽支持良好的.

    關于ViewStub的一點代碼剖析

    inflate vs setVisibility

    inflate和setVisibility的共同點是都可以實現加載布局

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 /*** When visibility is set to {@link #VISIBLE} or {@link #INVISIBLE},* {@link #inflate()} is invoked and this StubbedView is replaced in its parent* by the inflated layout resource.** @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.** @see #inflate() */@Overridepublic void setVisibility(int visibility) {if (mInflatedViewRef != null) {View view = mInflatedViewRef.get();if (view != null) {view.setVisibility(visibility);} else {throw new IllegalStateException("setVisibility called on un-referenced view");}} else {super.setVisibility(visibility);if (visibility == VISIBLE || visibility == INVISIBLE) {inflate();}}}

    setVisibility只是在ViewStub第一次延遲初始化時,并且visibility是非GONE時,調用了inflate方法.

    inflate源碼

    通過閱讀下面的inflate方法實現,我們將更加理解

    • android:inflatedId的用途
    • ViewStub在初始化后從視圖層級中移除
    • ViewStub的layoutParameters應用
    • mInflatedViewRef通過弱引用形式,建立ViewStub與加載的View的聯系.
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 /*** Inflates the layout resource identified by {@link #getLayoutResource()}* and replaces this StubbedView in its parent by the inflated layout resource.** @return The inflated layout resource.**/public View inflate() {final ViewParent viewParent = getParent();if (viewParent != null && viewParent instanceof ViewGroup) {if (mLayoutResource != 0) {final ViewGroup parent = (ViewGroup) viewParent;final LayoutInflater factory = LayoutInflater.from(mContext);final View view = factory.inflate(mLayoutResource, parent,false);if (mInflatedId != NO_ID) {view.setId(mInflatedId);}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);}mInflatedViewRef = new WeakReference<View>(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");}}

    關于ViewStub的研究就是這些,希望對大家關于優化視圖有所幫助和啟發.

    轉載于:https://my.oschina.net/u/2517174/blog/846203

    總結

    以上是生活随笔為你收集整理的Android中使用ViewStub提高布局性能的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。