android 按需加载视图
2015-08-17 13:40
337 查看
编写:allenlsy - 原文:http://developer.android.com/training/improving-layouts/loading-ondemand.html
有时你的 Layout 会用到不怎么重用的复杂视图。不管它是列表项 细节,进度显示器,或是撤销时的提示信息,你可以仅在需要的时候载入它们,提高 UI 渲染速度。
Layout 类型。
以下 ViewStub 是一个半透明的进度条覆盖层。功能上讲,它应该只在新的数据项被导入到应用程序时可见。
注意:
一旦 ViewStub 可见或是被 inflate 了,ViewStub 元素就不存在了。取而代之的是被 inflate 的 Layout,其 id 是 ViewStub 上的
注意:ViewStub 的一个缺陷是,它目前不支持使用
自:http://hukai.me/android-training-course-in-chinese/performance/improving-layouts/loading-ondemand.html
有时你的 Layout 会用到不怎么重用的复杂视图。不管它是列表项 细节,进度显示器,或是撤销时的提示信息,你可以仅在需要的时候载入它们,提高 UI 渲染速度。
定义 ViewStub
ViewStub 是一个轻量的视图,不需要大小信息,也不会在被加入的 Layout 中绘制任何东西。每个 ViewStub 只需要设置android:layout属性来指定需要被 inflate 的
Layout 类型。
以下 ViewStub 是一个半透明的进度条覆盖层。功能上讲,它应该只在新的数据项被导入到应用程序时可见。
<ViewStub android:id="@+id/stub_import" android:inflatedId="@+id/panel_import" android:layout="@layout/progress_overlay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" />
载入 ViewStub Layout
当你要载入用 ViewStub 声明的 Layout 时,要么用setVisibility(View.VISIBLE)设置它的可见性,要么调用其
inflate()方法。
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE); // or View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
注意:
inflate()方法会在渲染完成后返回被 inflate 的视图,所以如果你需要和这个 Layout 交互的话, 你不需要再调用
findViewById()去查找这个元素,。
一旦 ViewStub 可见或是被 inflate 了,ViewStub 元素就不存在了。取而代之的是被 inflate 的 Layout,其 id 是 ViewStub 上的
android:inflatedId属性。(ViewStub 的
android:id属性仅在 ViewStub 可见以前可用)
注意:ViewStub 的一个缺陷是,它目前不支持使用
<merge/>标签的 Layout 。
自:http://hukai.me/android-training-course-in-chinese/performance/improving-layouts/loading-ondemand.html
相关文章推荐
- Java or Android 知识点书签
- android中的与GPS定位的API
- android 音乐模仿器demo,实现指定目录下的音乐循环播放,和在线播放的方法。
- android关闭cursor的方法
- 【攻克Android (33)】四大组件之 BroadcastReceiver
- Android------以后进先出的方式加载图片
- 关于Android popupWindow的位置显示 showAtLocation理解
- Cordova android框架详解
- Android动画效果translate、scale、alpha、rotate详解
- Android(java)学习笔记168:Java异常分类
- 从零搭建 repo 服务器
- 我开发的第二个Android软件
- android 驱动文件权限设置
- Android 倒计时
- Android HAL JNI实例解析-led
- 从设计到实现,一步步教你实现Android-Universal-ImageLoader-辅助类
- Android 如何判断CPU是32位还是64位
- Android 开发 对话框Dialog dismiss和hide方法的区别
- 【译】Android主题动态切换开源库Prism基本原理2-搭配ViewPager使用
- 解决 android 的R文件不能生成 R cannot be resolved to a variable