Android development: optimize the analysis of ListView practices

Source: Internet
Author: User

Android development: optimize the analysis of ListView practices

After reading some vogella articles, I found that the performance optimization of android listview is very interesting. So I practiced it. After optimization, the performance is indeed improved a lot!

First look at the comparison before and after optimization:

Logs before optimization:

Optimized log:

In addition, ANR may occur before optimization in the Process of non-stop scrolling ListView, which is particularly easy to reproduce on AVD:

Then, the optimization looks very smooth, and the corresponding log is attached:

The relevant code analysis is attached below:

Each Item in ListView is composed of an ImageView and a TextView.

Layout:

 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Android: layout_width = "fill_parent"
  4. Android: layout_height = "fill_parent"
  5. Android: orientation = "horizontal">
  6. <ImageView android: id = "@ + id/imageView"
  7. Android: layout_width = "wrap_content"
  8. Android: layout_height = "fill_parent"/>"
  9. <TextView android: id = "@ + id/textView"
  10. Android: layout_width = "wrap_content"
  11. Android: layout_height = "fill_parent"
  12. Android: layout_marginLeft = "15dp"
  13. Android: gravity = "center_vertical"/>
  14. </LinearLayout>

The Activity is inherited from ListActivity. I intentionally added Item to facilitate testing and the effect is more obvious:

 
  1. Public class ListViewDemo extends ListActivity {
  2. Private final String [] mItems = new String [] {"Android", "iPhone ",
  3. "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7 ",
  4. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  5. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 ",
  6. "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu ",
  7. "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7 ",
  8. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  9. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 ",
  10. "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu ",
  11. "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7 ",
  12. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  13. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 ",
  14. "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu ",
  15. "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7 ",
  16. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  17. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 ",
  18. "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu ",
  19. "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7 ",
  20. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  21. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 ",
  22. "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu ",
  23. "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7 ",
  24. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  25. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 ",
  26. "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu ",
  27. "Windows7", "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7 ",
  28. "Max OS x", "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X ",
  29. "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS x", "Linux", "OS/2 "};
  30. @ Override
  31. Public void onCreate (Bundle savedInstanceState ){
  32. Super. onCreate (savedInstanceState );
  33. ListViewArrayAdapter adapter = new ListViewArrayAdapter (this, mItems );
  34. GetListView (). setAdapter (adapter );
  35. }
  36. }

Then, use the M Adapter to optimize the previous adapter:

 
  1. @ Override
  2. Public View getView (int position, View convertView, ViewGroup parent ){
  3. Long start = System. currentTimeMillis ();
  4. LayoutInflater inflater = (LayoutInflater) mContext. getLayoutInflater ();
  5. View rowView = inflater. inflate (mViewResourceId, parent, false );
  6. TextView textView = (TextView) rowView
  7. . FindViewById (mTextViewResourceId );
  8. ImageView imageView = (ImageView) rowView
  9. . FindViewById (mImageViewResourceId );
  10. TextView. setText (mNames [position]);
  11. String s = mNames [position];
  12. If (s. startsWith ("Windows7") | s. startsWith ("iPhone ")){
  13. ImageView. setImageResource (R. drawable. no );
  14. } Else {
  15. ImageView. setImageResource (R. drawable. yes );
  16. }
  17. Log. v ("jerikc", "cost time =" + (System. currentTimeMillis ()-start ));
  18. Return rowView;
  19. }

The optimized Adapter:

 
  1. Public class ListViewArrayAdapter extends ArrayAdapter <String> {
  2. Private final Activity mContext;
  3. Private final String [] mNames;
  4. Private final static int mViewResourceId = R. layout. text_image_row_layout;
  5. Private final static int mTextViewResourceId = R. id. textView;
  6. Private final static int mImageViewResourceId = R. id. imageView;
  7. Static class ViewHolder {
  8. Public TextView text;
  9. Public ImageView image;
  10. }
  11. Public ListViewArrayAdapter (Activity context, String [] names ){
  12. Super (context, mViewResourceId, names );
  13. This. mContext = context;
  14. This. mNames = names;
  15. }
  16. @ Override
  17. Public View getView (int position, View convertView, ViewGroup parent ){
  18. Long start = System. currentTimeMillis ();
  19. View rowView = convertView;
  20. If (rowView = null ){
  21. LayoutInflater inflater = mContext. getLayoutInflater ();
  22. RowView = inflater. inflate (mViewResourceId, null );
  23. ViewHolder viewHolder = new ViewHolder ();
  24. ViewHolder. text = (TextView) rowView. findViewById (mTextViewResourceId );
  25. ViewHolder. image = (ImageView) rowView. findViewById (mImageViewResourceId );
  26. RowView. setTag (viewHolder );
  27. }
  28. ViewHolder holder = (ViewHolder) rowView. getTag ();
  29. String s = mNames [position];
  30. Holder. text. setText (s );
  31. If (s. startsWith ("Windows7") | s. startsWith ("iPhone ")){
  32. Holder. image. setImageResource (R. drawable. no );
  33. } Else {
  34. Holder. image. setImageResource (R. drawable. yes );
  35. }
  36. Log. v ("jerikc", "cost time =" + (System. currentTimeMillis ()-start ));
  37. Return rowView;
  38. }
  39. }

The general idea of optimization is: before optimization, each time you load an item, you need to load the layout file and generate a new row View object, then we can find the corresponding ImageView and TextView through the View. As we know, loading layout files is time-consuming, especially when operations are frequent, therefore, ANR may occur.

Therefore, we can reuse invisible row View objects. In Android, when it decides to make the row View object invisible, it allows the convertView parameter in the getView method to reuse the previously invisible row View object.

During the optimization process, when loading for the first time, we need to save the relevant data, and the View has a method setTag, which can be used to save some data structures. A row View object consists of ImageView and TextView spaces. Therefore, a ViewHolder is defined to save ImageView and TextView objects. In the reuse process, you only need to modify their values, instead of the findViewById.

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.