Android optimized--ui optimization (iii)

Source: Internet
Author: User

1.ViewStubLazy Loading

Viewstub is an invisible, size 0 view, the best use is to implement the view delay loading, when needed to load the view, can be common in Java Performance optimization method lazy loading.
When the setvisibility function that calls Viewstub is set to visible or the call inflate initializes the view, the resource referenced by the viewstub begins to initialize, and then references the resource substitution viewstub its own location to populate the Viewstub location. Therefore, before calling the setvisibility (int) or inflate () function, a viewstub exists in the component tree hierarchy, but because Viewstub is very lightweight, this has a very small impact on performance. You can redefine the referenced layout ID by viewstub the Inflatedid property. For example:

    • Xml
<ViewStub        android:id="@+id/stub"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:inflatedId="@+id/subTree"        android:layout="@layout/activity_viewstub_item"/>
    • Java
//获取到viewstubfinal ViewStub stub = (ViewStub) findViewById(R.id.stub);//测试用inflate()填充布局new Handler() {    @Override    publicvoidhandleMessage(Message msg) {        View view = stub.inflate();    }};handler.sendEmptyMessageDelayed(17000);

I've been here a while to call the stub.inflate(); method, load the layout, and we don't have to go to Findviewbyid to load the view anymore.

    • Effect

Android optimized--ui optimization (iii)

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.