Android-Optimizing UI Performance (4)-Using viewstub

Source: Internet
Author: User

android-Optimizing UI Performance (4)-Using viewstub
viewstub Concept:
Viewstub is an invisible, lightweight view. It has no dimensions, does not draw and participates in the layout in some way. Only when the inflate is called will the view be instantiated.
This means that the cost of viewstub preserving the structure of the view hierarchy is very low.

1, delayed loading of infrequently used UI controls
When some controls are used only in a good number of cases, we can use the viewstub to delay loading
To increase the speed of the UI load and reduce memory consumption
Here is a demo:
Main activity class:

 Public  class mainactivity extends Activity{    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//Load button layoutSetcontentview (R.layout.layout_delay);//Create button objects and click eventsButton _button = (button) Findviewbyid (r.id.buttonload); _button.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View arg0) {//TODO auto-generated method stub                //Make the button invisibleArg0.setenabled (false);//Get Stub objectViewstub _viewstub = (viewstub) Findviewbyid (r.id.viewstubload);//Press into current layoutView inflate = _viewstub.inflate ();    }        }); }

Two layout files:
1,layout_delay.xml

<?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  =;     <viewstub  android:id  = "@+id/viewstubload"  android:layout_width
      = "wrap_content"  android:layout_height  =          "wrap_content"  android:inflatedid  = "@+id/viewstub" //Application Span class= "Hljs-attribute" >activity_main.xml  android:layout  = "@layout/activity_main" />     <buttonandroid:id="@+id/buttonload"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" Load " />                                 </linearlayout>

2,activity.xml

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="Match_parent"android:layout_height="Match_parent"Android:paddingbottom="@dimen/activity_vertical_margin"android:paddingleft="@dimen/activity_horizontal_margin"android:paddingright="@dimen/activity_horizontal_margin"android:paddingtop="@dimen/activity_vertical_margin"tools:context=". Mainactivity "> <textview android:layout_width="Wrap_content"android:layout_height="Wrap_content"android:text="@string/hello_world"/></relativelayout>

The results are as follows:

2, improve the speed of layout change
Need to use the scene: The interface requires frequent switching, improve the switching speed

How to use: To change the screen demo frequently to explain
1, set activity
Android:configchanged= "Keyboardhidden|orientation"

<activity android:name=". Myconfigurechangedactivity "android:configchanges=" Keyboardhidden|orientation ">                         <intent-filter>                <action android:name="Android.intent.action.MAIN" />                <category android:name="Android.intent.category.LAUNCHER" />            </intent-filter>        </activity>

2, write different layout for the screen
Land

<?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  =;     <textview  android:id  = "@+id/textviewland"  android:layout_width
      = "wrap_content"  android:layout_height  =         "wrap_content"  android:text  = "TextView" />     <SeekBarandroid:id= "@+id/seekbarland"android:layout_width=" Match_parent "android:layout_height=" Wrap_content "android:layout_weight=" 1 " />                                </linearlayout>

Such as:

Port

<?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  =;     <TextViewandroid:id= "@+id/textviewport"android:layout_width=" Wrap_content "android:layout_height="wrap_content "android:text=" Textviewport " />                                    <SeekBarandroid:id="@+id/seekbarport"android:layout_width= "Match_parent" android:layout_height="wrap_content" />                        </linearlayout>

Such as:

3, create a layout, and contain only two view stubs corresponding to the screen

<?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" >                <viewstub  android:id  = "@+id/stubland"  android:layout_width  = "wrap_content"  android:layout_height  = "wrap_content"  android:inflatedid  =< Span class= "Hljs-value" > "@+id/inflatestubland"  android:layout  = "@layout/layout_land" />     <viewstubandroid:id= "@+id/stubport"android:layout_width="Wrap_ Content "android:layout_height="wrap_content "android:inflatedid=" @+id/ Inflatestubport "android:layout=" @layout/layout_port "/>                                         </linearlayout>

4, at the time of the screen, create the current view by calling Viewstub.inflate and set the other one to gone

Private void Setorientationlayout(intOrientation) {//If the system state is in the horizontal direction        if(Orientation = = Configuration.orientation_landscape) {//No layout in horizontal direction, create layout            if(NULL= = Mlanderview) {viewstub _viewstub = (viewstub) Findviewbyid (R.id.stubland);            Mlanderview = _viewstub.inflate (); }//If the current layout is low vertical layout, set to invisible            if(NULL! = Mportview) {mportview.setvisibility (view.gone); }//Binding interface LayoutBindView (Mlanderview); }Else{//If the system state is in the vertical direction            //The current layout is a horizontal layout and is set to invisible            if(NULL! = Mlanderview) {mlanderview.setvisibility (view.gone); }//Vertical layout is empty, press into vertical layout            if(NULL= = Mportview) {viewstub _viewstub = (viewstub) Findviewbyid (R.id.stubport);            Mportview = _viewstub.inflate (); }//Binding interfaceBindView (Mportview); }    }

5, binding and setting the state of the control

//绑定函数    private void bindView(View p_v)    {        //要绑定的View设置为可见        p_v.setVisibility(View.VISIBLE);        //两个控件采用系统的布局        mSeekBar = (SeekBar)findViewById(android.R.id.progress);        mTextView = (TextView)findViewById(android.R.id.text1);    }

OnCreate

//onCreateprotectedvoidonCreate(Bundle savedInstanceState)    {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_land);        Display _disPlay = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();         //得到布局的方向        setOrientationlayout(_disPlay.getOrientation());    }

Copyright NOTICE: Welcome to communicate the error of the article, must humbly accept, QQ872785786

Android-Optimizing UI Performance (4)-Using viewstub

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.