In Android development, the ListView is an important component, and it is self-adapting to show specific content based on the length of the data in list form. The user is free to define the layout of each column in the ListView.
When a ListView has a large amount of data to load, it occupies memory and affects performance. This time, you need to populate on demand and re-use view to reduce the creation of objects.
The listview load data is performed in the GetView () method. Also, you need to customize the ListView rewrite ListAdapter: such as Baseadapter, Simpleadapter, CursorAdapter, and so on.
Optimize the ListView load speed to allow Convertview to match the list type and maximize the reuse of Convertview.
The GetView () method typically has the following three ways of loading:
(1) Slowest load mode: Redefine a view each time and load the layout and reload the data.
Public View GetView (int position, view Convertview, ViewGroup parent) {View item = Minflater. Inflate(R. Layout. List_item_icon_text, NULL);((TextView) item. Findviewbyid(R. ID. Text)). SetText(Data[position]);((ImageView) item. Findviewbyid(R. ID. Icon)). Setimagebitmap((Position &1) ==1? Micon1:micon2);Return item;}
(2) Faster load mode: When Convertview is not empty, the Convertview is reused directly, thus reducing unnecessary view creation and then loading the data.
Public View GetView (int position, View Convertview, ViewGroup parent) {if (Convertview = = null) {Conve Rtview = Minflater. Inflate(R. Layout. Item, parent, false);} ((TextView) Convertview. Findviewbyid(R. ID. Text)). SetText(Data[position]);((ImageView) Convertview. Findviewbyid(R. ID. Icon)). Setimagebitmap((Position &1) ==1? Micon1:micon2);Return Convertview;}
(3) The quickest way to do this: Define a viewholder, set the Convetview tag to Viewholder, and reuse the viewholder when not empty.
Static class Viewholder {TextView text;ImageView icon;Public View GetView (int position, View Convertview, ViewGroup parent) {Viewholder holder;if (Convertview = = null) {Convertview = Minflater. Inflate(R. Layout. List_item_icon_text, parent, false);Holder = new Viewholder ();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);Return Convertview;}
Three ways to compare efficiency:
When dealing with time-consuming resource loads, do the following 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 (update data source)
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android-viewholder Optimization ListView