Performance optimization 1:viewstub for Android application development

Source: Internet
Author: User

When developing our Android applications, depending on the product requirements, you may need to dynamically display a view or layout at runtime based on certain conditions. Perhaps the most common approach is to write all the possible view in the layout file, and first set the unwanted view.setvisibility () parameter to view. GONE (not visible), and then dynamically setvisibility (view.visible) in the code according to the needs shown. The advantages are: logic is simple, control is flexible. The downside is also obvious: resource-intensive.

A better approach is to use viewstub, which is a lightweight view: invisible, non-occupying layout, and very small resource-intensive controls. In our layout file for Viewstub to specify a layout, in the inflate layout, only viewstub will be initialized, and then when the viewstub is set to be visible, or call Viewstub.inflate (), The layout viewstub is inflate and instantiated, and the layout properties of the Viewstub are passed to the layout that it points to. You can use Viewstub to make it easy to display a layout at run time.

Obviously Viewstub is easy to use and optimized for performance, but it can't be omnipotent, say viewstub some features:

1. Viewstub can only be inflate once, and then viewstub objects will be empty. This means: Once the layout specified by the viewstub is inflate, it will not be able to control it through Viewstub, which is equivalent to making the layout visible in the viewstub under certain circumstances and cannot be made invisible.

2. Viewstub can only inflate a layout file, not a specific view (such as TextView), of course, you can write the view in a layout file.

So we usually consider using viewstub in the following cases:

1. During the run of the program, a layout will not change after inflate, unless it is restarted.

2. You want to control what is displayed and hidden is a layout file, not a view.

Here is a very simple example:

The first is a layout file containing Viewstub and Layout.activity_result:

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >
<relativelayout <!--initialize the layout to be displayed when the page is entered--
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:background= "#FFFFFF" >

<mageview
Android:id= "@+id/personal_page_iv"
Android:layout_width= "60DP"
android:layout_height= "60DP"
Android:layout_marginbottom= "15DP"
android:layout_marginleft= "15DP"
android:layout_marginright= "10DP"
android:layout_margintop= "15DP"
android:src= "@drawable/clear"/>
<textview
Android:id= "@+id/personal_page_name_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_margintop= "20DP"
android:layout_torightof= "@id/personal_page_iv"
Android:textcolor= "#333333"
Android:textsize= "14SP"
/>
<textview
Android:id= "@+id/personal_page_id_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/personal_page_name_tv"
android:layout_margintop= "5DP"
android:layout_torightof= "@id/personal_page_iv"
android:text= "ID:"
Android:textcolor= "#969696"
Android:textsize= "12SP"/>
<textview
Android:id= "@+id/personal_page_id_number_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/personal_page_name_tv"
android:layout_marginleft= "10DP"
android:layout_margintop= "5DP"
android:layout_torightof= "@id/personal_page_id_tv"
android:text= "8888888"
Android:textcolor= "#969696"
Android:textsize= "12SP"/>
<textview
Android:id= "@+id/personal_page_total_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/personal_page_id_tv"
android:layout_margintop= "5DP"
android:layout_torightof= "@id/personal_page_iv"
Android:textcolor= "#969696"
Android:textsize= "12SP"/>
<textview
Android:id= "@+id/personal_page_total_number_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/personal_page_id_number_tv"
android:layout_marginleft= "10DP"
android:layout_margintop= "5DP"
android:layout_torightof= "@id/personal_page_total_tv"
android:text= "245"
Android:textcolor= "#969696"
Android:textsize= "12SP"/>
</RelativeLayout>
<viewstub<!--Enter the page only if the data is loaded successfully to display the layout--
Android:id= "@+id/stub_pay_result_success"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:inflatedid= "@+id/subtree"
android:layout= "@layout/mystub_pay_result_fail"/>

</LinearLayout>

We then control the viewstub display in the code of our activity:

Class MyActivity extends activity{

Viewstub viewstub;

@Override
protected void OnCreate (Bundle savedinstancestate) {


Setcontentview (R.layout.activity_result);
Initviews ();//Initialize control, omit

Getserverdata ();//Get Server service corresponding data

private void Getserverdata () {

If successful, such as the sign of success is the Boolean flag=true;

if (flag) {

Viewstub = (viewstub) Findviewbyid (r.id.stub_pay_result_success);

View view = Viewstub.inflate ();
Then use Findviewbyid () to find the controls in the view to set the data

}

}

This allows the data to be displayed when it succeeds, and it is not easy to get the data if it fails.

Performance optimization 1:viewstub for Android application development

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.