Analysis of Android development optimization: a detailed explanation of Interface UI optimization (3)

Source: Internet
Author: User

Sometimes, our pages may contain some la S. These la s are hidden by default. After a user triggers some operations, the hidden la s are displayed. For example, an activity is used to display the list of friends. After you click "import" in the menu, a layout interface for importing friends is displayed in the current activity. From the perspective of requirements, this import function is generally not used by users. In most cases, the layout of imported friends is not displayed. In this case, you can use the delayed loading function.

Viewstub is a hidden view object that does not occupy memory space. It can load layout resource files at runtime delay. When viewstub is set to visible or the inflate () function is called, the layout resource file is loaded. This viewstub replaces itself in the parent container when loading the view. Therefore, viewstub will remain in the view until setvisibility (INT) or inflate () is called. The layout parameters of viewstub are added to the parent container of viewstub along with the number of loaded views. Similarly, you can use the inflated ID attribute to define or rename the id value of the view object to be loaded.

See the followingCodeFragment.

Copy code The Code is as follows: <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"/>

The ID "stub_import" can be used to find the defined viewstub object. After the layout resource file "progress_overlay" is loaded, the viewstub object is removed from its parent container. You can use the "panel_import" ID to find the view created by the layout resource "progress_overlay.

The recommended method for loading layout resource files is as follows:

Copy code The Code is as follows: (viewstub) findviewbyid (R. Id. stub_import). setvisibility (view. Visible );

// Or

View importpanel = (viewstub) findviewbyid (R. Id. stub_import). Inflate ();

When inflate () is called, this viewstub is replaced by the loaded view, and this view object is returned. This makes the applicationProgramYou do not need to execute findviewbyid () to obtain reference for loading a view.Copy codeThe Code is as follows:Experience Sharing:

Viewstub can be used to associate with the specified layout resource file in the XML file, so that the layout resource file can be loaded as needed. When to load data, instead of loading data at startup. This can speed up application startup and save memory resources.

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.