To optimize the ListView first to understand how it works, the display of the list requires three elements: ListView, Adapter, display data;
The adapter is used in the adapter mode, regardless of the incoming view in the ListView can be displayed.
The following simple principle:
1. If you have thousands of tens of thousands of or more options (item), only the visible items (fullscreen shows the item number) exist in memory (said optimization means in-memory optimization!). ), the other in the Recycler
2. The ListView first requests a type1 view (GetView) and then requests other visible items. Convertview in GetView is empty (null), the first time is empty, as long as the display of Convertview is not empty, will be saved in recycler
3. When item1 rolls out of the screen and a new item comes up from the low end of the screen, the ListView requests a type1 view. Convertview is not a null value at this time, its value is item1. You just need to set up new data and return to Convertview, without having to recreate a view, eliminating the time of inflate and Findviewbyid, and optimizing performance.
Once we understand how it works, we can reuse the Convertview and use it as long as it's not empty, changing its content.
A ListView is used with a adapter, in order to make the performance better, the ListView caches the line item (the view of a row). The ListView obtains the item for each line through the GetView function of the adapter.
[Java]View Plaincopy
- 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> ();
- @Override
- protected 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 < ; i++) {
- List.add ("item" + i);
- }
- Lv.setadapter (adapter);
- }
- private class ListAdapter extends Baseadapter {
- private Layoutinflater Minflater;
- ListAdapter () {
- Minflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
- }
- @Override
- public int GetCount () {
- //TODO auto-generated method stub
- return list.size ();
- }
- @Override
- Public Object GetItem (int position) {
- //TODO auto-generated method stub
- return List.get (position);
- }
- @Override
- public long getitemid (int position) {
- //TODO auto-generated method stub
- return position;
- }
- @Override
- Public View GetView (int position, View Convertview, ViewGroup parent) {
- //TODO auto-generated method stub
- System.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 elements in item
- *
- * @author Administrator
- *
- */
- public static class Viewholder {
- public TextView text;
- public ImageView Image;
- }
- }
Run effect
The result of the first print is Convertview null
Print after sliding the listview
As can be seen from the print message above, Recycler will save seven Convertview objects to display item, whether you have thousands of item, will only create display full screen Convertview, which greatly saves memory, The use of the Viewholder tag also greatly saves performance overhead.
The listview cache mechanism for Android performance optimization