Reprinted from: http://blog.csdn.net/pkxiuluo01/article/details/7380860
The adapter serves as a bridge between the listview interface and data. When each item in the list is displayed on the page, the getview method of the adapter is called to return a view. If there are many items in the list, it will take up a lot of system resources, so we need to optimize the adapter
1. Use of convertview
[Java]View plaincopy
- Java code
- Public View getview (INT position, view convertview, viewgroup parent ){
- Viewholder holder;
- If (convertview = NULL)
- {
- // Load the layout file of the listview item
- Convertview = minflater. Inflate (R. layout. list_item_icon_text, null );
- Holder = new viewholder ();
- Holder. Text = (textview) convertview. findviewbyid (R. Id. Text );
- Holder. Icon = (imageview) convertview. findviewbyid (R. Id. Icon );
- /**
- * Use holder to call the convertview. settag function.
- * The memory address created by convertview is not empty.
- * Call gettag to obtain the tag at the current position.
- * Finally, replace the tag at the current position with various set operations of holder.
- */
- Convertview. settag (holder );
- }
- Else
- {
- Holder = (viewholder) convertview. gettag ();
- }
- Holder. Text. settext (data [position]);
- Holder. Icon. setimagebitmap (Position & 1) = 1? Micon1: micon2 );
- Return convertview;
- }
- Static class viewholder {
- Private textview text;
- Private imageview icon;
- }
In the preceding method, a viewholder is used for each view to control its internal sub-items. The settag and gettag methods are also used to bind the holder to the view, instead of directly creating the view. optimized.
2. Test the number of getview calls
If you create a dynamically refreshed listview and find that the speed of the displayed Adapter. notifydatasetchanged () is a little slow, you can test whether the number of getview calls is too large. If the number of calls is too large, you may find it inexplicable. It is certainly a problem that will definitely affect the performance.
Change method:
XML Code
[HTML]View plaincopy
- <Listview Android: Id = "@ + ID/List"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"/>
Modify Android: layout_height = "wrap_content" to Android: layout_height = "fill_parent.
If it is useless after modification, and the control around listview is "wrap_content", you also need to modify it.
3. You can directly set backgroundcolor for items with solid color in listview instead of using images. In fact, this part can be greatly improved. Similarly, you should try to set RGB color for any solid color background instead use an image as the background.