Android development tips: viewstub inertia loading

Source: Internet
Author: User
Tags control label

This article is selected from the android Development Authority guide.

In section 4.5.6, we introduced a <include> label that can reference another layout file in the layout file and overwrite all Layout-related attributes of the root node of the referenced layout file, it is an attribute starting with Android: layout. The <include> label can be used to split a very large layout file into several smaller layout files, and these smaller layout files can be referenced multiple times, to achieve a purpose of reuse.

<Include> labels are good, but one problem is that the controls in the layout file are not always used when the program is started, some controls are used only under certain circumstances. For example, a software reading a book only needs to display a progress bar when downloading an e-book. It is a local e-book loaded during normal reading and does not need to use a progress bar. Therefore, you can not load this progress bar at program startup. However, when the <include> label is used to reference the layout file containing the progress bar, all the controls are loaded into the memory regardless of the time ranges. Some readers may say that a progress bar does not occupy much system resources and does not matter if it is loaded. These readers may be right, but if they do not load progress bars, but many imageview controls (displaying large images), and they are not loaded in one place, I am afraid that the poor mobile phone resources will be exhausted. Therefore, we urgently need a mechanism to change the behavior of the <include> label, only when the control needs to be loaded. This mechanism is the viewstub Control described in this section.

Viewstub is an invisible control. It serves basically the same purpose as the <include> label and uses the <viewstub> label in the layout file to reference other layout files. However, the only difference from <include> is that viewstub does not immediately load the referenced layout file. Viewstub loads the referenced control only after the viewstub. inflate or viewstub. setvisibility (view. Visible) method is called. The following describes the two layout files.

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 = "my buttons"

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 = "button 1"/>

<Buttonandroid: layout_width = "fill_parent"

Android: layout_height = "wrap_content" Android: text = "button 2"/>

</Linearlayout>

The <include> label is used in the main. xml file to reference custom. xml. In this case, three buttons shown in 5.56 are displayed on the screen immediately. If you replace the <include> tag with the following code, the <include> tag will only display the definition button in the main. xml file when the program starts, as shown in 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"/>

Figure 5.56 use the <include> label to load controls

Figure 5.56 use the <viewstub> label to load controls

After you use the <viewstub> label to reference the layout file, you also need to call the viewstub. inflate or viewstub. setvisibility (view. Visible) method to load the referenced control. The Code is as follows:

Public void onclick_button (view V)

{

// The viesstub control can only be obtained once. If you use findviewbyid to obtain the viewstub object for the second time, null is returned.

View view = findviewbyid (R. Id. viewstub );

If (view! = NULL)

{

// Or call the viewstub. Inflate Method

// View = (viewstub) view). Inflate ();

// Load the control in the viewm. xml file referenced by viewstub

(Viewstub) view). setvisibility (view. Visible );

}

Else

{

Settitle ("view is null ");

}

}

After you click "my button", the two buttons defined in the custom. xml file are displayed. The effect is exactly the same as that in Figure 5.56.

Note: like the <viewstub> label, you can set all the layout-related attributes of the root node in the referenced layout file. The difference is that the Android: Id attribute of the <include> tag directly overwrites the Android: Id attribute value of the root node in the referenced layout file, and the Android: the ID attribute is the same as the Android: Id attribute of a common control label. It is used to reference a control in code. In the <viewstub> label, you must use the Android: inflatedid attribute to overwrite the Android: Id attribute value of the root node in the referenced layout file. Although <viewstub> can completely replace <include>, the only drawback is that <viewstub> cannot replace <merge>.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.