您好,登錄后才能下訂單哦!
Android中怎么利用ViewStub提高布局性能,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
ViewStub是什么
ViewStub是View的子類
它不可見(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)簽
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns: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"> <Button android:id="@+id/clickMe" android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ViewStub android: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布局
ViewStub myViewStub = (ViewStub)findViewById(R.id.myViewStub); if (myViewStub != null) { myViewStub.inflate(); //或者是下面的形式加載 //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不支持
關(guān)于不支持
驗(yàn)證一:直接 標(biāo)簽
如下,我們有布局文件名為merge_layout.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Yes"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="No"/> </merge>
替換對(duì)應(yīng)的ViewStub的android:layout屬性值之后,運(yùn)行后(點(diǎn)擊Button按鈕)得到產(chǎn)生了如下的崩潰
E AndroidRuntime: android.view.InflateException: Binary XML file line #1: <merge /> can be used only with a valid ViewGroup root and attachToRoot=true E 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=true E AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:491) E AndroidRuntime: ... 13 more
可見(jiàn),直接的
驗(yàn)證二 間接的ViewStub
下面布局間接使用了merge標(biāo)簽.文件名為 include_merge.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"> <include layout="@layout/merge_layout"/> </LinearLayout>
然后修改ViewStub的 android:layout 值,運(yùn)行,一切正常.
除此之外,本例也驗(yàn)證了ViewStub也是對(duì)
關(guān)于ViewStub的一點(diǎn)代碼剖析
inflate vs setVisibility
inflate和setVisibility的共同點(diǎn)是都可以實(shí)現(xiàn)加載布局
/** * 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() */ @Override public 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***次延遲初始化時(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)系.
/** * 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"); } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。