Android中使用ViewStub实现布局优化
在android开发中,view是我们必须要接触的用来展示的技术.通常情况下随着view视图的越来越复杂,整体布局的性能也会随之下降.这里介绍一个在某些场景下提升布局性能的view,它就是viewstub.
viewstub是什么
- viewstub是view的子类
- 它不可见,大小为0
- 用来延迟加载布局资源
注,关于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中,桩是指用来代替关联代码或者未实现代码的代码.
viewstub使用场景
如上图所示,
- 一个listview包含了诸如 新闻,商业,科技 等item
- 每个item又包含了各自对应的子话题,
- 但是子话题的view(蓝色区域)只有在点击展开按钮才真正需要加载.
- 如果默认加载子话题的view,则会造成内存的占用和cpu的消耗
所以,这时候就viewstub就派上用处了.使用viewstub可以延迟加载布局资源.
viewstub 怎么用
在布局文件中使用viewstub标签
<?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); }
关于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
<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>
替换对应的viewstub的android:layout属性值之后,运行后(点击button按钮)得到产生了如下的崩溃
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
可见,直接的 <merge> 标签,viewstub是不支持的.
验证二 间接的viewstub
下面布局间接使用了merge标签.文件名为 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 值,运行,一切正常.
除此之外,本例也验证了viewstub也是对 <include> 标签支持良好的.
关于viewstub的一点代码剖析
inflate vs setvisibility
inflate和setvisibility的共同点是都可以实现加载布局
/** * 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第一次延迟初始化时,并且visibility是非 gone 时,调用了 inflate 方法.
inflate源码
通过阅读下面的inflate方法实现,我们将更加理解
- android:inflatedid的用途
- viewstub在初始化后从视图层级中移除
- viewstub的layoutparameters应用
- minflatedviewref通过弱引用形式,建立viewstub与加载的view的联系.
/** * 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的研究就是这些,希望对大家关于优化视图有所帮助和启发.
上一篇: Android 开发系统自带语音模块应用
推荐阅读
-
Android中使用ViewStub实现布局优化
-
Android App中的GridView网格布局使用指南
-
Android中RecyclerView布局代替GridView实现类似支付宝的界面
-
Android布局优化之ViewStub控件
-
Android布局技巧之使用ViewStub
-
Android手机开发 使用线性布局和相对布局实现Button垂直水平居中
-
Android App中的GridView网格布局使用指南
-
Android中RecyclerView布局代替GridView实现类似支付宝的界面
-
Android布局技巧之使用ViewStub
-
Android布局优化之ViewStub控件