Layout tips: Using viewstub

Source: Internet
Author: User

Thanks to the <include/> tag, on Android, it's easy to share and reuse UI components. In Android development, it's easy to create a complex UI structure, which results in a lot of view, and some of it is rarely used. In response to this, thank goodness, Android also provides us with a special component--viewstub, which allows you to fully enjoy the benefits of <include/> Without creating useless view waste.

Viewstub is an invisible, lightweight view. It has no dimensions, does not draw and participates in the layout in some way. This means that the cost of viewstub to inflate and retain it in the view hierarchy is cheap. Viewstub's best description is called "lazy include." The layout referenced in Viewstub is displayed only when you want to add it to the UI.

The following comes from the shelves application. The activity shown in the figure is a list of books that are presented to the user for viewing:

The same activity is also used for users to add or import new books. In this operation, shelves shows an additional UI. The following shows a progress meter and a Cancel button at the bottom of the screen during import:

Because importing a book is not a common operation, at least relative to browsing the book list, the import panel is hosted by Viewstub:

When a user makes an import operation, viewstub is inflate, and the layout file content that it references is replaced by the display:

In order to use viewstub, all you need to do is specify the Android:id feature, which is convenient for later inflate, specifying the Android:layout attribute, referencing the layout file. Viewstub also allows you to use the third attribute, Android:inflatedid, which you can use to override the ID of the root element contained in the layout file. Finally, the layout_* parameter set on the viewstub will be applied to the top of the included layout file. Here's an example:

<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"/>

When you are ready to inflate viewstub, call the Inflate () method. You can also set the viewstub visibility to be visible or invisible, and also trigger inflate. Note that the inflate () method is used to return the root view of the layout file:

((viewstub) Findviewbyid (R.id.stub_import)). Setvisibility (view.visible);
Or
View Importpanel = ((viewstub) Findviewbyid (R.id.stub_import)). Inflate ();

One thing to remember: when Viewstub inflate, the viewstub is removed from the view hierarchy. Therefore, it is not necessary to keep a reference to Viewstub (as in the field of the class).

Viewstub is the product of fast programming and efficient programming. Instead of manually inflate view and adding it to the view hierarchy at run time, it's better to simply use Viewstub. It is fairly "inexpensive" and easy to use. Viewstub The only drawback is that <merge/> tags are not supported at this time.

Layout tips: Using viewstub

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.