Android Application development performance optimization 1: ViewStub

Source: Internet
Author: User

Android Application development performance optimization 1: ViewStub

When developing our Android Application, you may need to dynamically display a View or Layout at runtime based on certain conditions. The most common method is to write all the views that may be used in the layout file. First, set the unwanted View. setVisibility () parameter to View.GONE (invisible)And dynamically setVisibility (View. VISIBLE) according to the display needs in the code ). In this way, the logic is simple and the control is flexible. The disadvantage is also obvious: resource consumption.

A better way is to useViewStubIt is a lightweight View: invisible, does not occupy the layout location, occupies a very small resource control. In our layout file, specify a layout for ViewStub. In the Inflate layout, only ViewStub will be initialized. Then, when ViewStub is set to visible, or ViewStub is called. when inflate () is used, the layout directed by ViewStub will be Inflate and instantiated, and the layout attribute of ViewStub will be transmitted to the layout pointed to by it. You can use ViewStub to check whether a layout is displayed during running.

Obviously ViewStub is easy to use and optimized in terms of performance, but it cannot be omnipotent. Let's talk about some characteristics of ViewStub:

1. ViewStub can only be Inflate once, and then the ViewStub object will be left blank. It means that after a layout specified by ViewStub is Inflate, it will not be enough to control it through ViewStub, which is equivalent to making the layout of ViewStub visible under specific circumstances, it cannot be invisible again.

2. ViewStub can only Inflate a layout file, rather than a specific View (such as TextView). Of course, you can write the View in a layout file.

Therefore, ViewStub is usually used in the following scenarios:

1. during the running of the program, a layout after Inflate will not change unless it is restarted.

2. To control the display and hiding of a layout file, rather than a View.

The following is a simple example:

The layout file layout. activity_result contains ViewStub:


Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">

Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "# FFFFFF">

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


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


Then we control the ViewStub display in the code of the Activity:

Class MyActivity extends Activity {

ViewStub viewStub;

@ Override
Protected void onCreate (Bundle savedInstanceState ){


SetContentView (R. layout. activity_result );
InitViews (); // initialization control, omitted

GetServerData (); // get the Server Service Data

}

Private void getServerData (){

// If the result is obtained successfully, for example, the flag is boolean flag = true;

If (flag ){

ViewStub = (ViewStub) findViewById (R. id. stub_pay_result_success );

View view = viewStub. inflate ();
// Then use findViewById () to find the control in the view and set the data.

}

}

}

In this way, the data can be displayed when the acquisition is successful. If the acquisition fails, the data will not be displayed. Isn't it easy?

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.