Android Performance Optimization: Viewstub

Source: Internet
Author: User

This is often the case when developing an application that dynamically determines which view or layout to display at run time, depending on the condition. The most common idea is to write the view that is likely to be used, first to set the visibility of the view. GONE, and then dynamically change its visibility in the code. The advantage of this approach is that logic is simple and control is more flexible. But the disadvantage is that it consumes resources. Although the view is initially visible. GONE But the view will still be inflate when the inflate is laid out, that is, the object will still be created, instantiated, and the property will be set. In other words, it consumes resources such as memory.

The recommended practice is to use the Android.view.ViewStub, Viewstub is a lightweight view that occupies very small resources of the control. In the inflate layout, only viewstub will be initialized, and then when Viewstub is set to visible ( default is invisible), or when Viewstub.inflate () is called, The layout that viewstub will be inflate and instantiated.

But Viewstub is not omnipotent, the following summarizes what viewstub can do and when to use the viewstub, when the visibility of the control.

First of all, some features of viewstub:

(1) Viewstub can only be inflate once, and then Viewstub object will be empty. According to the sentence, a viewstub specified by the layout is inflate, it will not be enough to control it through the viewstub.

(2) Viewstub can only be used to inflate a layout file, not a specific view, of course, you can also write the view in a layout file.

Based on the above characteristics, the use of viewstub can be considered as follows:

(1) During the operation of the program, a layout after inflate, there will be no change, unless restarted.

Because viewstub can only be inflate once and then be empty, you cannot expect to use Viewstub to control the layout later. So when you need to show and hide a layout more than once at run time, Viewstub can't. You can only use view to control it.

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

Because the ID of a layout file can only be set to viewstub, it cannot be controlled for a view.

So, if you want to control the display and concealment of a view (such as button or TextView), or if you want to show and hide a layout or view at run time, you can only use view to control it.

Let's look at an example

In this example, to display two different layouts, one is to display a piece of text with TextView, and the other is to display a picture with ImageView. These two are at OnCreate () to decide which one to display, this is the best place to apply viewstub.

Let's take a look at the layout, one is the main layout, which only defines two viewstub, one to control the TextView one to control ImageView, and the other is a TextView layout for the display text, one for the ImageView layout:

   

 1 <?xml version= "1.0"  encoding= "Utf-8"? >   2 <linearlayout    3     xmlns:android= "Http://schemas.android.com/apk/res/android"    4     android:orientation= "Vertical"    5      android:layout_width= "Fill_parent"    6     android: layout_height= "Fill_parent"    7     android:gravity= "center_horizontal ">   8     <ViewStub    9          android:id= "@+id/viewstub_demo_text"   10          android:layout_width= "Wrap_content"   11          android:layout_height= "Wrap_content"   12          android:layout_marginleft= "5dip"   13         android: layout_marginright= "5dip"   14         android:layout_ margintop= "10dip"   15         android:layout= "@layout/ Viewstub_demo_text_layout "/>  16     <viewstub   17          android:id= "@+id/viewstub_demo_image"   18          android:layout_width= "Wrap_content"   19          android:layout_height= "Wrap_content"   20          android:layout_marginleft= "5dip"   21          android:layout_marginright= "5dip"   22          android:layout= "@layout/viewstub_demo_image_layout"/>  23 </linearlayout> 

 1 // ... 2      @Override    3      public void oncreate (bundle savedinstancestate)  {   4          super.oncreate (savedinstancestate);   5          setcontentview (r.layout.viewstub_demo_activity);    6         if  ((((int)   (math.random ()  *  ())  & 0x01)  == 0)  {   7              ViewStub stub =  (viewstub)  findviewbyid ( R.id.viewstub_demo_text); //  Get layout  8              stub.inflate (); //  instantiation  9   10              textview text =  (TextView)  findviewbyid (R.id.viewstub_demo_textview);   11             text.settext ("Hello  world ");  12         } else {   13             ViewStub stub  =  (viewstub)  findviewbyid (r.id.viewstub_demo_image);  14              stub.inflate ();15 16              ImageView image =  (ImageView)  findviewbyid ( R.id.viewstub_demo_imageview);  17              image.setimageresource (r.drawable.img);  18          }  19 &nbSP;&NBSP;&NBSP;&NBSP;}20&NBSP;//&NBSP;&NBSP, ..... 

There are similar new content, new feature hints, this will only be displayed once, and will not change the viewstub is quite suitable for


Android Performance Optimization: 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.