[Android performance optimization series] layout: dynamic loading layout and android Performance Optimization

Source: Internet
Author: User

[Android performance optimization series] layout: dynamic loading layout and android Performance Optimization

If you like my blog, please pay attention to my Weibo, please click here (http://weibo.com/kifile), thank you

Reprinted please indicate the source (http://blog.csdn.net/kifile), thank you again


Address: http://developer.android.com/training/improving-layouts/loading-ondemand.html

In the next period of time, I will translate some official Android documents about performance improvement every day for you.

Performance Optimization Layout:

[Android performance optimization series] layout to reduce your interface level

[Android performance optimization series] layout article <include> reuse Layout

Digress:

Many times, we add some infrequently used controls to the layout file, but we seldom use them, but they consume system resources during initialization, therefore, we should consider optimizing ViewStub, as stated in the memory article, to use it only when it is actually needed.


The following is the body of this article:

################

Sometimes, some widgets in your layout are complex but rarely used. Whether it's Message Details, progress bars, or incomplete prompts, you can load them until they are actually needed to reduce your memory consumption and improve rendering efficiency.


Define a ViewStub

ViewStub is a lightweight View with no size. It does not draw anything on the layout or do other work. This means that initialization and retaining it at the layout level only takes a small amount of money. In addition, remember that each ViewStub must contain an android: layout attribute to indicate that the layout will be displayed.

The following ViewStub develops a translucent progress bar, which can be seen only when a new item is added to the application:

<ViewStub    android:id="@+id/stub_import"    android:inflatedId="@+id/panel_import"    android:layout="@layout/progress_overlay"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:layout_gravity="bottom" />

Load the layout of ViewStub

When you want to load the layout specified by ViewStub, you can choose to use setVisiblity (View. VISIBLE) or call the inflate () method.

((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);// orView importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();

Note: The inflate () method returns the initialized View. Therefore, you do not need to use findViewById to find this View.

Once the specified layout is displayed or initialized, The ViewStub element is no longer part of the layout level and will be replaced by the specified layout, the id of the specified layout is specified in the ViewStub attribute through android: inflatedId (and this id is only valid after the specified layout is displayed or initialized)

Note: ViewStub Cannot initialize the layout file of the <merge/> label.


Ps: I have been very lazy and busy recently, so I have never updated anything. In fact, this article was translated almost half a month ago, but it seems very simple and has not been sent out. But after thinking about it, I reorganized the text and finally sent it out, the layout article should have a part about ListView optimization, but I feel that everyone has always been useful. I just want to simply state his thoughts here. The optimization of ListView mainly lies in the reuse of layout during getView and the use of ViewHolder to save the required controls, so as to avoid searching through findViewById every time you use it, if you are interested, you can go to the official address to see: http://developer.android.com/training/improving-layouts/smooth-scrolling.html. So the layout is here. Thank you for reading this article.

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.