In Android development, the ListView control is an advanced control that is frequently used. It is usually used to display a series of highly similar data. When the data volume is large or the layout is quite complex, the performance optimization of ListView is very important. Therefore, in development, not only functions must be satisfied, but also performance cannot be sloppy.
Using ListView is a standard Adapter design mode. ListView combines data, XML layout, and Adapter to complete a series of display functions. In the data display of ListView, each sliding ListView triggers the call of the getView () method, and the XML definition layout is also in the getView () of the adapter () layoutInflater is used to load LayoutInflater. LayoutInflater is a resource-consuming task, and the findViewById () method is a time-consuming operation () the findViewById () operation and findViewById () operation will bring serious performance problems to the application, so the performance optimization of ListView is urgent. Fortunately, Google engineers have provided us with corresponding solutions, the cache function of ListView.
Let's take a look at the example:
The following is a simple Demo to explain the above description.
Package com. hitech. convertholderdemo; import java. util. arrayList; import java. util. list; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. checkBox; import android. widget. listView; import android. widget. textView; public class MainActivity extends Activity {public stati C final String TAG = "MainActivity"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ListView listView = (ListView) findViewById (R. id. lv_activity_main); listView. setAdapter (new ListViewDataAdapter ();} private class ListViewDataAdapter extends BaseAdapter {private List <String> list = new ArrayList <String> (); public ListViewDataAdapter () {for (int I = 0; I <100; I ++) {list. add (I + "") ;}@overridepublic int getCount () {return list. size () ;}@ Overridepublic Object getItem (int position) {return list. get (position) ;}@ Overridepublic long getItemId (int position) {return 0 ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {View view = null; viewHolder holder = null; if (convertView! = Null) {// If convertView has a cache view = convertView; // directly use the cache and retrieve the cached ViewHolder object holder = (ViewHolder) view. getTag (); Log. e (TAG, "convertView with cache" + position);} else {Log. e (TAG, "convertView without cache" + position); // if no cache is available, load the Layout view = getLayoutInflater () from the XML file (). inflate (R. layout. dialog_popup_main_activity, null); // cache the control in the loading layout to ViewHolder. holder = new ViewHolder (); holder. textview_username = (TextView) view. findViewById (R. id. textview_username); holder. textview_nickname = (TextView) view. findViewById (R. id. textview_nickname); holder. checkbox_selected = (CheckBox) view. findViewById (R. id. checkbox_selected); // Save the holder object as data to viewview. setTag (holder);} // set controls in the layout because of holder. textview_nickname.setText (list. get (position); holder. textview_username.setText (list. get (position); holder. checkbox_selected.setText (list. get (position); return view;} public static class ViewHolder {// encapsulate TextView textview_username = null for controls in the layout file based on object-oriented ideas; TextView textview_nickname = null; checkBox checkbox_selected = null ;}}