当前位置:操作系统 > 安卓/Android >>

Android开发技巧:ViewStub惰性装载

 

本文截选自《Android开发权威指南》

      在4.5.6节介绍过一个<include>标签,该标签可以在布局文件中引用另外一个布局文件,并可以覆盖被引用布局文件根节点所有与布局相关的属性,也就是以android:layout开头的属性。通过<include>标签可以将一个非常庞大的布局文件分解成若干个较小的布局文件,而且这些小的布局文件也可以被多次引用,从而达到一个重用的目的。

     <include>标签固然很好用,但有一个问题,就是布局文件中的控件并不一定在程序启动时全都用到,有一些控件只在特定的情况下才会被使用到。例如,一个阅读图书的软件只有在下载电子书时才需要显示进度条,在平时看书时都是装载的本地电子书,并不需要使用进度条。因此,在程序启动时完全可以先不加载这个进度条。但使用<include>标签引用这个包含进度条的布局文件时,不管三七二十一,将所有的控件全部装载到了内存中。也许有的读者会说,一个进度条占用不了多少系统资源,都装载也无所谓。这些读者也许是对的,但如果装载的不是进度条,而是很多ImageView控件(显示了很大的图像),并且还不是在一个地方装载,那恐怕就会将可怜的手机资源消耗殆尽了。因此,我们急需一种机制来改变<include>标签的这种行为,只在需要时装载控件。这种机制就是本节要介绍的ViewStub控件。

ViewStub是不可视的控件,它的作用与<include>标签基本相同,在布局文件中使用<ViewStub>标签来引用其他的布局文件。但与<include>唯一的不同是ViewStub并不会马上装载引用的布局文件。只有在调用了ViewStub.inflate或ViewStub.setVisibility(View.VISIBLE)方法后,ViewStub才会装载引用的控件,下面先看两个布局文件。

main.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:orientation="vertical"android:layout_width="fill_parent"

   android:layout_height="fill_parent">

   <Button android:layout_width="fill_parent"

       android:layout_height="wrap_content" android:text="我的按钮"

       android:onClick="onClick_Button" />

    <includelayout="@layout/custom" />

</LinearLayout>

custom.xml

<?xml version="1.0"encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

          android:orientation="vertical"android:layout_width="fill_parent"

          android:layout_height="fill_parent">

              <Buttonandroid:layout_width="fill_parent"

                      android:layout_height="wrap_content" an  droid:text="按钮1" />

              <Buttonandroid:layout_width="fill_parent"

                      android:layout_height="wrap_content" android:text="按钮2" />

</LinearLayout>

       在main.xml文件中使用了<include>标签来引用custom.xml,在这种情况下,屏幕上会立即显示三个如图5.56所示的按钮。如果将<include>标签换成如下的代码,在程序启动时,只会显示在main.xml文件中的定义按钮,如图5.57所示。

<ViewStub android:id="@+id/viewstub"android:inflatedId="@+id/button_layout"

   android:layout="@layout/custom"android:layout_width="fill_parent"

   android:layout_height="wrap_content"  />
\
              图5.56  使用<include>标签装载控件 

\

            图5.56  使用<ViewStub>标签装载控件
 

       在使用<ViewStub>标签引用布局文件后,还需要调用ViewStub.inflate或ViewStub.setVisibility(View.VISIBLE)方法才能装载所引用的控件,代码如下:

public void onClick_Button(View v)

{

    //  ViesStub控件只能获得一次,第二次再使用findViewById获得该ViewStub对象,则返回null  

    View view= findViewById(R.id.viewstub);

    if (view!= null)

    {   

       //  或调用ViewStub.inflate方法

        // view = ((ViewStub) view).inflate();

       //  装载ViewStub引用的custom.xml文件中的控件

       ((ViewStub)view).setVisibility(View.VISIBLE);

    }

    else

    {

       setTitle("view is null");

    }

}

单击“我的按钮”后,会显示在custom.xml文件中定义的两个按钮,效果与图5.56完全一样。

   注意:<ViewStub>与<include>标签一样,也可以设置所引用布局文件中根节点所有与布局相关的属性。所不同的是<include>标签的android:id属性直接覆盖了所引用布局文件中根节点的android:id属性值,而<ViewStub>标签的android:id属性与普通控件标签的android:id属性一样,用于在代码中引用控件。在<ViewStub>标签中需要使用android:inflatedId属性覆盖所引用布局文件中根节点的android:id属性值。虽然<ViewStub>可完全取代<include>,但唯一的不足是<ViewStub>目前还无法取代<merge>

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,