Android performance optimization: ListView caching mechanism, androidlistview
To optimize ListView, you must first understand how it works. The display of the list requires three elements: ListView, Adapter, and displayed data;
The Adapter is used in the Adapter mode, No matter what View is passed in, it can be displayed in the ListView.
The principle is as follows:
1. If you have thousands or even tens of thousands of items, only visible items are displayed (the number of items displayed on the full screen) memory exists (optimization means Optimization in memory !) In the Recycler.
2. ListView first requests a type1 view (getView) and then requests other visible projects. ConvertView is empty (null) in getView. It is empty for the first time. As long as convertView is not empty, it will be saved in Recycler.
3. When item1 gets out of the screen and a new project comes from the low end of the screen, ListView then requests a type1 view. ConvertView is not a null value. Its value is item1. You only need to set new data and return convertView. You do not need to create a new view, saving the time of inflate and findViewById, and thus optimizing the performance.
After understanding how it works, we can reuse convertView. If it is not empty, we can directly use it and change its content.
When using ListView, an Adapter is used. In order to improve the performance, ListView caches the row item (View corresponding to a row ). ListView obtains the items of each row through the getView function of the Adapter.
Package com. dzt. listviewdemo; import java. util. arrayList; import android. app. activity; import android. content. context; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. baseAdapter; import android. widget. imageView; import android. widget. listView; import android. widget. textView; public class MainActivity extends Activity {private ListAdapter adapter; private ListView lv = null; private ArrayList <String> list = new ArrayList <String> (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); lv = (ListView) findViewById (R. id. lv_list); adapter = new ListAdapter (); for (int I = 0; I <100; I ++) {list. add ("item" + I);} lv. setAdapter (adapter);} private class ListAdapter extends BaseAdapter {private LayoutInflater mInflater; ListAdapter () {mInflater = (LayoutInflater) getSystemService (Context. LAYOUT_INFLATER_SERVICE);} @ Overridepublic int getCount () {// TODO Auto-generated method stubreturn list. size () ;}@ Overridepublic Object getItem (int position) {// TODO Auto-generated method stubreturn list. get (position) ;}@ Overridepublic long getItemId (int position) {// TODO Auto-generated method stubreturn position ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubSystem. out. println ("getView" + position + "" + convertView); viewHolder holder = null; if (convertView = null) {convertView = mInflater. inflate (R. layout. item, null); holder = new viewHolder (); holder. text = (TextView) convertView. findViewById (R. id. TV _text); holder. image = (ImageView) convertView. findViewById (R. id. iv_img); convertView. setTag (holder);} else {holder = (viewHolder) convertView. getTag ();} holder. text. setText (list. get (position); if (position % 2 = 0) {holder. image. setImageResource (R. drawable. ic_launcher);} else {holder. image. setImageResource (R. drawable. icon);} return convertView;}/*** use a class to save the element ** @ author Administrator **/public static class viewHolder {public TextView text; public ImageView image ;}}
Running Effect
The convertView result printed for the first time is null.
Print after sliding ListView
From the printed message above, we can see that the Recycler will save seven convertView objects to display items. No matter you have thousands of items, only convertView with full screen display will be created, this greatly saves the memory, and the use of viewHolder tags greatly saves the performance overhead.
Download related code: http://download.csdn.net/detail/deng0zhaotai/7842885
How does android listview clear memory in real time?
Android provides the listview cache mechanism to send your getview code.
If there is an attempt disorder, it is a problem with your code.
How to cache too many items in listview
The Listview itself has a view reuse mechanism. Why is there 1000 data records in the data cache? What applications need to display such a large amount of data? Can I optimize the data source first? The processing and storage capabilities of the Android mobile phone itself are not strong. It is necessary for you to process so much data that only two users can see it? Do not do things that affect the user experience.