The listview cache mechanism for Android performance optimization

Source: Internet
Author: User

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
  1. Package Com.dzt.listviewdemo;
  2. Import java.util.ArrayList;
  3. Import android.app.Activity;
  4. Import Android.content.Context;
  5. Import Android.os.Bundle;
  6. Import Android.view.LayoutInflater;
  7. Import Android.view.View;
  8. Import Android.view.ViewGroup;
  9. Import Android.widget.BaseAdapter;
  10. Import Android.widget.ImageView;
  11. Import Android.widget.ListView;
  12. Import Android.widget.TextView;
  13. Public class Mainactivity extends Activity {
  14. private ListAdapter adapter;
  15. private ListView LV = null;
  16. private Arraylist<string> list = new arraylist<string> ();
  17. @Override
  18. protected void OnCreate (Bundle savedinstancestate) {
  19. super.oncreate (savedinstancestate);
  20. Setcontentview (R.layout.activity_main);
  21. LV = (ListView) Findviewbyid (r.id.lv_list);
  22. adapter = new ListAdapter ();
  23. For (int i = 0; i < ; i++) {
  24. List.add ("item" + i);
  25. }
  26. Lv.setadapter (adapter);
  27. }
  28. private class ListAdapter extends Baseadapter {
  29. private Layoutinflater Minflater;
  30. ListAdapter () {
  31. Minflater = (layoutinflater) getsystemservice (Context.layout_inflater_service);
  32. }
  33. @Override
  34. public int GetCount () {
  35. //TODO auto-generated method stub
  36. return list.size ();
  37. }
  38. @Override
  39. Public Object GetItem (int position) {
  40. //TODO auto-generated method stub
  41. return List.get (position);
  42. }
  43. @Override
  44. public long getitemid (int position) {
  45. //TODO auto-generated method stub
  46. return position;
  47. }
  48. @Override
  49. Public View GetView (int position, View Convertview, ViewGroup parent) {
  50. //TODO auto-generated method stub
  51. System.out.println ("GetView" + position + "" + Convertview);
  52. Viewholder holder = null;
  53. if (Convertview = = null) {
  54. Convertview = Minflater.inflate (R.layout.item, null);
  55. Holder = new Viewholder ();
  56. Holder.text = (TextView) Convertview.findviewbyid (R.id.tv_text);
  57. Holder.image = (ImageView) convertview
  58. . Findviewbyid (R.ID.IV_IMG);
  59. Convertview.settag (holder);
  60. } Else {
  61. Holder = (viewholder) convertview.gettag ();
  62. }
  63. Holder.text.setText (List.get (position));
  64. if (position% 2 = = 0) {
  65. Holder.image.setImageResource (R.drawable.ic_launcher);
  66. } Else {
  67. Holder.image.setImageResource (R.drawable.icon);
  68. }
  69. return convertview;
  70. }
  71. }
  72. /** 
  73. * Use a class to save the elements in item
  74. *
  75. * @author Administrator
  76. *
  77. */
  78. public static class Viewholder {
  79. public TextView text;
  80. public ImageView Image;
  81. }
  82. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.