Loading Views On Demand

THIS LESSON TEACHES YOU TO

  1. Define a ViewStub
  2. Load the ViewStub Layout

YOU SHOULD ALSO READ

Sometimes your layout might require complex views that are rarely used. Whether they are item details, progress indicators, or undo messages, you can reduce memory usage and speed up rendering by loading the views only when they are needed.

用ViewStub引用的嵌入的layout可推迟加载

Define a ViewStub


  ViewStub is a lightweight view with no dimension and doesn’t draw anything or participate in the layout. As such, it's cheap to inflate and cheap to leave in a view hierarchy. Each ViewStub simply needs to include the android:layout attribute to specify the layout to inflate.

The following ViewStub is for a translucent progress bar overlay. It should be visible only when new items are being imported into the application.

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" />

Load the ViewStub Layout


  When you want to load the layout specified by the ViewStub, either set it visible by calling setVisibility(View.VISIBLE) or call inflate().

加载ViewStub对应的layout的2方法
1,((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
2,View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

Note: The inflate() method returns the inflated View once complete. so you don't need to call findViewById() if you need to interact with the layout.

注意 作用第2种方法时,如果该ViewStub对应的lahout是临时的,那么可以不用 findViewById()

Once visible/inflated, the ViewStub element is no longer part of the view hierarchy. It is replaced by the inflated layout and the ID for the root view of that layout is the one specified by the android:inflatedId attribute of the ViewStub. (The ID android:id specified for the ViewStub is valid only until the ViewStub layout is visible/inflated.)

一旦ViewStub被加载,那么ViewStub就被其指定的layout替换。

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

Layout Resource官方教程(2)用ViewStub引用的嵌入的layout可推迟加载的更多相关文章

  1. Layout Resource官方教程(3)在layout中用include嵌入其它layout

    简介 <include>Includes a layout file into this layout. 类似 #include ,把layout展开在include处 attribute ...

  2. Layout Resource官方教程(1)简介

    Layout Resource SEE ALSO Layouts A layout resource defines the architecture for the UI in an Activit ...

  3. Layout Resource官方教程(4)<include>与<merge>

    Re-using Layouts with <include/> THIS LESSON TEACHES YOU TO Create a Re-usable Layout Use the ...

  4. Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  5. 从零开始实现ASP.NET Core MVC的插件式开发(六) - 如何加载插件引用

    标题:从零开始实现ASP.NET Core MVC的插件式开发(六) - 如何加载插件引用. 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p/1171 ...

  6. img引用网络图片资源无法加载问题解决

    近期在自己项目中遇到引用一些网络图片资源,显示无法加载,但是在浏览器打开图片路径又可以显示的问题 解决办法: 在图片显示的界面把meta referrer标签改为never <meta name ...

  7. 自定义View(7)官方教程:自定义View(含onMeasure),自定义一个Layout(混合组件),重写一个现有组件

    Custom Components In this document The Basic Approach Fully Customized Components Compound Controls ...

  8. Ceisum官方教程2 -- 项目实例(workshop)

    原文地址:https://cesiumjs.org/tutorials/Cesium-Workshop/ 概述 我们很高兴欢迎你加入Cesium社区!为了让你能基于Cesium开发自己的3d 地图项目 ...

  9. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

随机推荐

  1. oc 一些通用函数

    1 i= 0,1,2... unichar c = [self characterAtIndex:i]; //取出i这个位置对应的字符 2 拼凑字符串 [NSString stringWithForm ...

  2. jquery个人笔记

    一.链式操作 <!DOCTYPE html> <html> <head> <title></title> <script src = ...

  3. [译]CSS content

    原文地址:http://css-tricks.com/css-content/ CSS中有一个属性content,只能和伪元素:before和:after一起使用,他们的写法像伪类选择器(前面有冒号) ...

  4. ajax和jsonp的封装

    一直在用jQuery的ajax,跨域也是一直用的jQuery的jsonp,jQuery确实很方便,$.ajax({...})就可以搞定. 为了更好的理解ajax和jsonp,又重新看了下书,看了一些博 ...

  5. CentOS7 firewall的使用

    # 查看区域 firewall-cmd --get-zones # 查看默认区域 firewall-cmd --get-default-zone # 给区域添加永久性服务 firewall-cmd - ...

  6. SSH调试

    <s:date>标签中若是用date数组或Calendar数组,则永远显示数组最后一个数. 试试List.Set.Map也不行. 看来只能够使用单个对象.或者在后台传送String 数组, ...

  7. NetBeans中文乱码解决办法

    一.Windows下NetBeans中文乱码解决办法 找到你的Netbeans安装目录下的etc文件夹,用记事本打开netbeans.conf,找到netbeans_default_options(不 ...

  8. ECSHOP 订单状态 记录

    记录订单状态 order_status /* 订单状态 */ define(‘OS_UNCONFIRMED’,            0); // 未确认 define(‘OS_CONFIRMED’, ...

  9. 我写了个教程《一步步教你把ubuntu安装到U盘》

    一步步教你把ubuntu安装到U盘 作者 Val 2452013147@qq.com 原因: 由于某些原因(学生党),需要把ubuntu安装到U盘到处走,百度了一下,教程都不是很好,要么很复杂,要么不 ...

  10. 【prism】前期准备

    在网上下了prism框架源码,目前最新版本为4.1,其中包含的内容如下: 其中包含三类文件: 1.类似于Desktop only-Prism Library.bat的批处理文件,用来打开相应的Pris ...