日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
Android使用ViewStub提高布局性能

在Android開(kāi)發(fā)中,View是我們必須要接觸的用來(lái)展示的技術(shù).通常情況下隨著View視圖的越來(lái)越復(fù)雜,整體布局的性能也會(huì)隨之下降.這里介紹一個(gè)在某些場(chǎng)景下提升布局性能的View,它就是ViewStub.

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)網(wǎng)站空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、銀川網(wǎng)站維護(hù)、網(wǎng)站推廣。

ViewStub是什么

  • ViewStub是View的子類(lèi)
  • 它不可見(jiàn),大小為0
  • 用來(lái)延遲加載布局資源

注,關(guān)于Stub的解釋

A stub is a small program routine that substitutes for a longer program, possibly to be loaded later or that is located remotely

在Java中,樁是指用來(lái)代替關(guān)聯(lián)代碼或者未實(shí)現(xiàn)代碼的代碼.

ViewStub使用場(chǎng)景

如上圖所示,

  • 一個(gè)ListView包含了諸如 新聞,商業(yè),科技 等Item
  • 每個(gè)Item又包含了各自對(duì)應(yīng)的子話題,
  • 但是子話題的View(藍(lán)色區(qū)域)只有在點(diǎn)擊展開(kāi)按鈕才真正需要加載.
  • 如果默認(rèn)加載子話題的View,則會(huì)造成內(nèi)存的占用和CPU的消耗

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

ViewStub 怎么用

1.在布局文件中使用ViewStub標(biāo)簽

 
 
 
 
  1.  
  2.         xmlns:android="http://schemas.android.com/apk/res/android" 
  3.         xmlns:tools="http://schemas.android.com/tools" 
  4.         android:layout_width="match_parent" 
  5.         android:layout_height="match_parent" 
  6.         android:paddingLeft="@dimen/activity_horizontal_margin" 
  7.         android:paddingRight="@dimen/activity_horizontal_margin" 
  8.         android:paddingTop="@dimen/activity_vertical_margin" 
  9.         android:paddingBottom="@dimen/activity_vertical_margin" 
  10.         tools:context="com.droidyue.viewstubsample.MainActivity"> 
  11.  
  12.     
  13.             android:id="@+id/clickMe" 
  14.             android:text="Hello World!" 
  15.             android:layout_width="wrap_content" 
  16.             android:layout_height="wrap_content"/> 
  17.      
  18.     
  19.             android:id="@+id/myViewStub" 
  20.             android:inflatedId="@+id/myInflatedViewId" 
  21.             android:layout="@layout/include_merge" 
  22.             android:layout_width="wrap_content" 
  23.             android:layout_height="wrap_content" 
  24.             android:layout_below="@id/clickMe" 
  25.     /> 
  26.  

2.在代碼中inflate布局

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

關(guān)于ViewStub的事

  • 除了 inflate 方法外,我們還可以調(diào)用 setVisibility() 方法加載布局文件
  • 一旦加載布局完成后,ViewStub會(huì)從當(dāng)前布局層級(jí)中刪除
  • android:id 指定ViewStub ID,用于查找ViewStub進(jìn)行延遲加載
  • android:layout 延遲加載布局的資源id
  • android:inflatedId 加載的布局被重寫(xiě)的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不支持 標(biāo)簽.

關(guān)于不支持 標(biāo)簽的程度,我們進(jìn)行一個(gè)簡(jiǎn)單的驗(yàn)證

驗(yàn)證一:直接 標(biāo)簽

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

 
 
 
 
  1.  
  2.  
  3.     
  4.             android:layout_height="wrap_content" 
  5.             android:text="Yes"/> 
  6.  
  7.     
  8.             android:layout_height="wrap_content" 
  9.             android:text="No"/> 
  10.  
  11.  

 

替換對(duì)應(yīng)的ViewStub的android:layout屬性值之后,運(yùn)行后(點(diǎn)擊Button按鈕)得到產(chǎn)生了如下的崩潰

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

可見(jiàn),直接的 標(biāo)簽,ViewStub是不支持的.

驗(yàn)證二 間接的ViewStub

下面布局間接使用了merge標(biāo)簽.文件名為 include_merge.xml

 
 
 
 
  1.  
  2.               android:orientation="vertical" 
  3.               android:layout_width="match_parent" 
  4.               android:layout_height="match_parent"> 
  5.          
  6.  

然后修改ViewStub的 android:layout 值,運(yùn)行,一切正常.

除此之外,本例也驗(yàn)證了ViewStub也是對(duì) 標(biāo)簽支持良好的.

關(guān)于ViewStub的一點(diǎn)代碼剖析

inflate vs setVisibility

inflate和setVisibility的共同點(diǎn)是都可以實(shí)現(xiàn)加載布局

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

setVisibility只是在ViewStub***次延遲初始化時(shí),并且visibility是非 GONE 時(shí),調(diào)用了 inflate 方法.

inflate源碼

通過(guò)閱讀下面的inflate方法實(shí)現(xiàn),我們將更加理解

  • android:inflatedId的用途
  • ViewStub在初始化后從視圖層級(jí)中移除
  • ViewStub的layoutParameters應(yīng)用
  • mInflatedViewRef通過(guò)弱引用形式,建立ViewStub與加載的View的聯(lián)系.

 

 
 
 
 
  1. /**     * Inflates the layout resource identified by {@link #getLayoutResource()} 
  2.      * and replaces this StubbedView in its parent by the inflated layout resource. 
  3.      * 
  4.      * @return The inflated layout resource. 
  5.      * 
  6.      */ 
  7.     public View inflate() { 
  8.         final ViewParent viewParent = getParent(); 
  9.  
  10.         if (viewParent != null && viewParent instanceof ViewGroup) { 
  11.             if (mLayoutResource != 0) { 
  12.                 final ViewGroup parent = (ViewGroup) viewParent; 
  13.                 final LayoutInflater factory = LayoutInflater.from(mContext); 
  14.                 final View view = factory.inflate(mLayoutResource, parent, 
  15.                         false); 
  16.  
  17.                 if (mInflatedId != NO_ID) { 
  18.                     view.setId(mInflatedId); 
  19.                 } 
  20.  
  21.                 final int index = parent.indexOfChild(this); 
  22.                 parent.removeViewInLayout(this); 
  23.  
  24.                 final ViewGroup.LayoutParams layoutParams = getLayoutParams(); 
  25.                 if (layoutParams != null) { 
  26.                     parent.addView(view, index, layoutParams); 
  27.                 } else { 
  28.                     parent.addView(view, index); 
  29.                 } 
  30.  
  31.                 mInflatedViewRef = new WeakReference(view); 
  32.  
  33.                 if (mInflateListener != null) { 
  34.                     mInflateListener.onInflate(this, view); 
  35.                 } 
  36.  
  37.                 return view; 
  38.             } else { 
  39.                 throw new IllegalArgumentException("ViewStub must have a valid layoutResource"); 
  40.             } 
  41.         } else { 
  42.             throw new IllegalStateException("ViewStub must have a non-null ViewGroup viewParent"); 
  43.         } 
  44.     } 

網(wǎng)站題目:Android使用ViewStub提高布局性能
網(wǎng)站路徑:http://m.5511xx.com/article/dhigjjo.html