Android Development Tips Viewstub control lazy load _android

Source: Internet
Author: User
A <include> tag is introduced in 4.5.6, which can refer to another layout file in a layout file, and can overwrite all layout-related properties of the referenced layout file root node, which is the property that starts with Android:layout. By <include> tag, a very large layout file can be decomposed into several smaller layout files, and these small layout files can be referenced multiple times to achieve a reuse purpose.

<include> tags are very useful, but there is a problem is that the layout of the control in the file is not necessarily used in the start of the program, there are some controls only in certain cases will be used. For example, a book-reading software only needs to display a progress bar when downloading an ebook, a local ebook that is loaded when reading a book, and does not need to use a progress bar. Therefore, the progress bar can be completely loaded without loading at the start of the program. However, when you use the <include> tag to refer to the layout file that contains the progress bar, all the controls are loaded into memory, regardless of 3,721. Perhaps some readers will say that a progress bar can not take up a lot of system resources, load also does not matter. These readers may be right, but if the load is not a progress bar, but a lot of ImageView control (showing a large image), and is not in a place to load, it will be the poor use of mobile phone resources exhausted. Therefore, we urgently need a mechanism to change the behavior of <include> tags, and only load controls when needed. This mechanism is the Viewstub control described in this verse.

Viewstub is a control that is not seen, and it functions as a <include> tag and uses <ViewStub> tags in the layout file to refer to other layout files. But the only difference with <include> is that viewstub does not immediately load the referenced layout file. Only when the Viewstub.inflate or viewstub.setvisibility (view.visible) method is invoked does viewstub load the referenced control, look at two layout files first.
Main.xml
Copy Code code 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 Button"
android:onclick= "Onclick_button"/>
<includelayout= "@layout/custom"/>
</LinearLayout>

Custom.xml
Copy Code code 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> tag is used in the Main.xml file to refer to Custom.xml, in which case the three buttons shown in Figure 5.56 are immediately displayed on the screen. If you replace the <include> tag with the following code, the definition button in the Main.xml file will be displayed at the start of the program, as shown in Figure 5.57.
Copy Code code 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 Loading controls with <include> tags


Figure 5.56 Loading controls with <ViewStub> tags
After you use the <ViewStub> tag to refer to the layout file, you also need to call the Viewstub.inflate or viewstub.setvisibility (view.visible) method to mount the referenced control, as follows:
Copy Code code as follows:

Publicvoidonclick_button (VIEWV)
{
The Viesstub control can only be obtained once, and the second time the Viewstub object is obtained using Findviewbyid, then returns null
Viewview=findviewbyid (r.id.viewstub);
if (view!=null)
{
or call the Viewstub.inflate method
view= (viewstub) view). Inflate ();
Load controls in the Custom.xml file referenced by viewstub
((viewstub) view). Setvisibility (view.visible);
}
Else
{
Settitle ("Viewisnull");
}
}

When you click my buttons, the two buttons that are defined in the Custom.xml file appear exactly as shown in Figure 5.56.
Attention:<viewstub> As with <include> labels, you can also set all layout-related properties of the root node in the referenced layout file. The difference is that the Android:id property of the <include> tag directly overrides the Android:id attribute value of the root node in the referenced layout file, while the <ViewStub> tag's Android: The ID property, like the Android:id property of the normal control label, is used to refer to the control in code. The Android:id property value of the root node in the referenced layout file needs to be overwritten with the Android:inflatedid property in the <ViewStub> tab. While <ViewStub> can completely replace <include&gt, the only disadvantage is that <ViewStub> is not yet a replacement for <merge>.

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.