Android development skills-ViewStub control inertia loading

Source: Internet
Author: User
Tags control label

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. xmlCopy codeThe Code is as follows: <? Xmlversion = "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">
<Buttonandroid: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "my buttons"
Android: onClick = "onClick_Button"/>
<Includelayout = "@ layout/custom"/>
</LinearLayout>

Custom. xmlCopy codeThe Code is as follows: <? Xmlversion = "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">
<Buttonandroid: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: 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.Copy codeThe Code is as follows: <ViewStubandroid: 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:Copy codeThe Code is as follows: publicvoidonClick_Button (Viewv)
{
// The ViesStub control can only be obtained once. If you use findViewById to obtain the ViewStub object for the second time, null is returned.
Viewview = 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 ("viewisnull ");
}
}

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 also 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.