Improve the efficiency of Android apps-mainly explain the optimization of listview

Source: Internet
Author: User
The adapter is the intermediary between the listview and the data source.

When each piece of data enters the visible area, the getview () of the adapter is called and a view representing the specific data is returned. It is called frequently when you touch and scroll. Supports hundreds of thousands of data entries.

The following is an XML file that displays each piece of data:

<Linearlayout
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "horizontal">
<Imageview Android: Id = "@ + ID/icon"
Android: layout_width = "48dip"
Android: layout_height = "48dip"/>
<Textview Android: Id = "@ + ID/text"
Android: layout_gravity = "center_vertical"
Android: layout_width = "0dip"
Android: layout_weight = "1.0"
Android: layout_height = "wrap_content"/>
</Linearlayout>

1. The simplest and least practical method

Public View getview (INT POs, view convertview,
Viewgroup parent ){
View Item = minflater. Inflate (R. layout. list_item, null );
(Textview) item. findviewbyid (R. Id. Text )).
Settext (data [POS]);
(Imageview) item. findviewbutid (R. Id. Icon )).
Setimagebitmap (Pos & 1) = 1? Micon1: micon2 );
Return item;
}

2. Convertview is used to reclaim views, improving the efficiency by 200%.

Public View getview (INT POs, view convertview,
Viewgroup parent ){
If (convertview = NULL ){
Convertview = minflater. Inflate (
R. layout. list_item, null );
}
(Textview) convertview. findviewbyid (R. Id. Text )).
Settext (data [POS]);
(Imageview) convertview. findviewbutid (R. Id. Icon )).
Setimagebitmap (Pos & 1) = 1? Micon1: micon2 );
Return convertview;
}

3. Using viewholder mode, the efficiency is improved by 50%

Static class viewholder {
Textview text;
Imageview icon;
}

Public View getview (INT POs, view convertview, viewgroup parent ){
Viewholder holder;
If (convertview = NULL ){
Convertview = minflater. Inflate (R. layout. list_item, null );
Holder = new viewholder ();
Holder. Text = (textview) convertview. findviewbyid (
R. Id. Text ));
Holder. Icon = (imageview) convertview. findviewbutid (
R. Id. Icon ));
Convertview. settag (holder );
} Else {
Holder = (viewholder) convertview. gettag ();
}
Holder. Text. settext (data [POS]);
Holder. Icon. setimagebitmap (Pos & 1) = 1? Micon1: micon2 );
Return convertview;
}

Adapter update efficiency comparison:

1 Updated less than 10 frames/second

2 updates close to 30 frames/second

Update 3 is close to 40 frames/second

Background and Image

View background images always fill the entire view area

1. Auto scaling is caused by improper image size.

2. Avoid real-time scaling

3. It is best to zoom in to the View Size in advance

originalimage = bitmap. createscaledbitmap (
originalimage, //

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.