In Android development, the ListView is a very important component, it is in the form of a list of data based on the long adaptive presentation of the content, the user can freely define the layout of each column of the ListView, but when the ListView has a large amount of data to load, will occupy a lot of memory, affect performance, you need to populate and re-use view on demand to reduce the creation of objects.
The ListView load data is in the public View getView (int position, view Convertview, ViewGroup parent) {} method ( To customize a ListView, you need to override ListAdapter:
such as Baseadapter,simpleadapter,cursoradapter's Getvview method), optimizing the load speed of the ListView will let Convertview match the list type, And to the maximum extent of re-use of 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
Public View GetView (int position, view Convertview, ViewGroup parent) { null); ((TextView) Item.findviewbyid (R.id.text)). SetText (Data[position]); & 1) = = 1? micon1:micon2); return item;}
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
Public View GetView (int position, view Convertview, ViewGroup parent) { ifnull) { false ); } ((TextView) Convertview.findviewbyid (R.id.text)). SetText (Data[position]); & 1) = = 1? micon1:micon2); return Convertview;}
The quickest way is to define a viewholder, set the Convetview tag to Viewholder, and not be reused when empty.
Static classViewholder {TextView text; ImageView icon;} PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder holder; if(Convertview = =NULL) {Convertview= Minflater.inflate (R.layout.list_item_icon_text,parent,false); Holder=NewViewholder (); Holder.text=(TextView) Convertview.findviewbyid (R.id.text); Holder.icon=(ImageView) Convertview.findviewbyid (R.id.icon); Convertview.settag (holder); } Else{Holder=(Viewholder) Convertview.gettag (); } holder.text.setText (Data[position]); Holder.icon.setImageBitmap ((Position& 1) = = 1?micon1:micon2); returnConvertview;}
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 change in the thread of the main interface and call the Notifydatasetchanged () method
ListView Load Performance Optimization Viewholder