In the early days of the Android project, one of the features is to crawl the news information on a site, with a ListView display, although the pagination, but still feel that the ideal smooth effect.
Internet access to some of the information, found some very good summary, recorded here, easy to review.
When a ListView has a large amount of data to load, it takes up a lot of memory and affects performance.
After testing, it is found that a lot of resources are in the ListView to load the layout file, that is, when the Findviewbyid, then we should consider how to reuse this layout file object, to reduce the creation of objects.
The ListView load data is in public View getView (int position, view Convertview, ViewGroup parent) {} method (when we go to customize the adapter, such as Baseadapter,simpleadapter,cursoradapter and other Getvview methods), optimizing the load speed of the ListView will allow Convertview to match the list type, and maximize the reuse Convertview.
The GetView loading method generally has the following three ways:
The slowest way to load is to redefine a view load layout every time, and then load the data
1 PublicView GetView (intposition, View Convertview, ViewGroup parent) {2 3View item = minflater.inflate (R.layout.list_item_icon_text,NULL);4 5 (TextView) Item.findviewbyid (R.id.text)). SetText (Data[position]);6 7 (ImageView) Item.findviewbyid (R.id.icon)). Setimagebitmap (8 9(position & 1) = = 1?micon1:micon2);Ten One returnitem; A -}
The correct way to load is when the Convertview is not empty and the convertview is reused to reduce the creation of many unnecessary view, then load the data
1 PublicView GetView (intposition, View Convertview, ViewGroup parent) {2 if(Convertview = =NULL) {3Convertview = Minflater.inflate (R.layout.item, parent,false);4 }5 (TextView) Convertview.findviewbyid (R.id.text)). SetText (Data[position]);6 (ImageView) Convertview.findviewbyid (R.id.icon)). Setimagebitmap (7(position & 1) = = 1?micon1:micon2);8 returnConvertview;9}
The quickest way is to define a Viewholder (object holding Class), set the Convetview tag to Viewholder, and not be reused when empty.
1 Static classViewholder {2 TextView text;3 ImageView icon;4 }5 6 PublicView GetView (intposition, View Convertview, ViewGroup parent) {7 Viewholder Holder;8 9 if(Convertview = =NULL) {TenConvertview =minflater.inflate (R.layout.list_item_icon_text, OneParentfalse); AHolder =NewViewholder (); -Holder.text =(TextView) Convertview.findviewbyid (r.id.text); -Holder.icon =(ImageView) Convertview.findviewbyid (R.id.icon); the Convertview.settag (holder); -}Else { -Holder =(Viewholder) Convertview.gettag (); - } + Holder.text.setText (data[position]); -Holder.icon.setImageBitmap ((position & 1) = = 1?micon1:micon2); + returnConvertview; A}
Three ways to load efficiency are shown in the following example:
Description: The above three example code excerpt from the Google I/O conference
When dealing with time-consuming resource loads, the following points need to be made to make your load faster and smoother:
1. The adapter is modified in the main thread of the interface
2. Data can be obtained from anywhere but the data should be requested in another place
3. Commit the adapter changes in the thread of the main interface and call the Notifydatasetchanged () method (update data source)
Android Development note--listview load performance optimization Viewholder