50 Android development skills (02 delayed loading and avoiding repeated rendering views)

Source: Internet
Author: User

When you create a Complex layout in the Application, the page rendering process becomes slower. In this case, we need to optimize our pages by using the <include/> label (avoid repeated rendering) and ViewStub class (delayed loading. (Original address: http://blog.csdn.net/vector_yi/article/details/24402101)
I. Use the <include/> label to avoid repeated Rendering
What do you do when we need to add a header or footer for each View in the App? Duplicate copy and paste can solve this problem, but it is complicated. You can try to use the <include/> label:
First, specify the width and height: main. xml in the <include/> label.

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"    android:layout_width= "fill_parent"    android:layout_height= "fill_parent" >    <Button        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:layout_gravity ="center_vertical"        android:onClick ="onShowMap"        android:text ="@string/show_map" />    <include        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:layout_alignParentBottom ="true"        android:layout_marginBottom ="30dp"        layout ="@layout/footer" /></RelativeLayout>

Footer. xml
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"    android:layout_width= "0dp"    android:layout_height= "0dp"    android:gravity= "center"    android:text= "@string/footer_text" />

Note that in footer. xml, we set both width and height to 0dp in combination with the width and height definitions in the <include/> label.
Method 2: directly reference: main. xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"    android:layout_width= "fill_parent"    android:layout_height= "fill_parent" >    <Button        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:layout_gravity ="center_vertical"        android:onClick ="onShowMap"        android:text ="@string/show_map" />    <include layout ="@layout/footer" /></RelativeLayout>

Footer. xml
<TextView xmlns:android = "http://schemas.android.com/apk/res/android"    android:layout_width= "fill_parent"    android:layout_height= "wrap_content"    android:layout_alignParentBottom= "true"    android:layout_marginBottom= "30dp"    android:gravity= "center"    android:text= "@string/footer_text" />

II. The ViewStub class is used to delay loading a view. When designing a view, sometimes the visibility of some views depends on the user's operation or the specific environment of the running device. How do you design it now? Is it just to change the visible attribute of the View? Let's take a look at ViewStub's introduction: ViewStub is an invisible zero-sized control that can be used to load view resources at runtime. Only when we set the ViewStub visibility to true or call the inflate () method will its view resources be loaded.
Suppose a page we designed contains a map View. Imagine the following situation: some users do not need to see the map. Since users do not need to see a map, why are we still persistently loading it? This affects the Performance of our apps. Now we can use the ViewStub class: main. xml
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"    android:layout_width= "fill_parent"    android:layout_height= "fill_parent" >    <Button        android:layout_width ="fill_parent"        android:layout_height ="wrap_content"        android:layout_gravity ="center_vertical"        android:onClick ="onShowMap"        android:text ="@string/show_map" />    <ViewStub        android:id ="@+id/map_stub"        android:layout_width ="fill_parent"        android:layout_height ="fill_parent"        android:inflatedId ="@+id/map_view"        android:layout ="@layout/map" /></RelativeLayout>

Map. xml
<com.google.android.maps.MapView xmlns:android ="http://schemas.android.com/apk/res/android"    android:layout_width= "fill_parent"    android:layout_height= "fill_parent"    android:apiKey= "my_api_key"    android:clickable= "true" />
Next let's take a look at MainActivity
public class MainActivity extends MapActivity {  private View mViewStub;  @Override  public void onCreate (Bundle savedInstanceState ) {    super. onCreate( savedInstanceState );    setContentView( R. layout. main);    mViewStub = findViewById( R. id. map_stub);  }  public void onShowMap (View v) {    mViewStub. setVisibility (View .VISIBLE );  }....}

As you can see, we call onShowMap to change the visibility of map_stub only when the image needs to be displayed. Before the changes, map_stub will not render and load view resources.
Summary: 1. When our pages become complex and there are too many XML files, the <include/> label can effectively help us sort the file content and improve the readability of the XML file. At the same time, its usage is similar to Fragment. 2. ViewStub is an excellent way to load view resources with latency. You can use ViewStub class as long as the designed view is dependent on context to change its visibility. Maybe when you only apply it to a simple page, you don't feel any improvement in performance, but in complex pages, it works very well.

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.