сряда, май 29, 2013

Android ListView Optimizations Checklist


These are gathered around the web and some Google IO videos in YouTube and are not ordered by importance.

1. Always reuse convertView - it can improve the performance of ListView scrolling up to 50-60% percents, especially when ListView items are complex

2. Use the Holder pattern if possible

3. android:smoothScrollbar="false"
Remove real-time computation of the scrollbar thumb size while scrolling

4. android:cacheColorHint="#hexcolor_equal_to_background_color_of_listview"
It's best if the color matches the background color of the ListView (or the Layout behind the ListView if the ListView is transparent or doesn't have background color)

5. android:background="#hexcolor"
By default, every Android Window already has a background which comes from the theme. It's always drawn and after that are drawn the layouts from the layouts.xml files that you define for your Activity. So if you specify a background for the ListView (or the Layout behind the ListView in case ListView background is missing or is transparent), then you have two backgrounds - one from the theme and the one that you specified in the XML. Both of these will be drawn. This is called "overdraw" and is unnecessary and takes more time. So instead of defining android:background="@color/my_background" for the ListView (or Layout behind the ListView), it's better to define it for the theme that is automatically created for your application in /res/values/styles.xml and leave the ListView without background property.
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="AppTheme" parent="@android:style/Theme.Light">
      <item name="android:windowBackground">@color/my_background</item>
   </style>
</resources>

6. android:hardwareAcceleration="true" 
Read carefully here and here before using

7. Avoid using wrap_content for view dimensions (this includes all views and sub-views in a list item)

8. Never use ListView or GridView inside a ScrollView

9. If you have stable Ids in your dataset implement the hasStableIds() method in the adapter to return true

10. Inflate views by using the provided parent in the getView method.

if(convertView == null) {
   convertView = inflater.inflate(R.layout.item_layout, parent, false);
}

11. Use Hierarchy Viewer to analyze your View tree. Make it as shallow and simple as possible. Each view or viewGroup that you have in your final list view item has to be measured and drawn and this takes time.

Video Sources
Google IO 2010 - The world of ListView
http://www.youtube.com/watch?v=wDBM6wVEO70

Google IO 2013 - Android Graphics Performance
http://www.youtube.com/watch?v=vQZFaec9NpA

Няма коментари: