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, //