These tips are gathered from articles and Google IO videos. The items are not ordered by importance.
1. Reuse convertView
Always reuse convertView. This can improve the performance of ListView scrolling up to 50-60% percent, especially when ListView items are complex.
2. Use the Holder pattern if possible.
3. Remove realtime computation of the scrollbar thumb size
|
|
4. Cache Color Hint
Make cached color hint match the background of the ListView (or the color of the layout behind the ListView in case the ListView is transparent)
|
|
5. Handle optimally ListView background color
layout.xml
files that are defined for Activities.If a background for the ListView is explicitly specified, then we have two backgrounds that will be drawn one after the other. This is called overdraw and is unnecessary and takes more time.
If a background for the ListView is missing or is transparent, but a parent layout behind the ListView has explicitly specified a background color, then again we have two backgrounds.
To avoid the overdraw it’s better to define the background color of the app only as a theme property called windowBackground
and leave the ListView without background color.
/res/values/styles.xml
6. Consider using hardware acceleration
Consider if using android:hardwareAcceleration
may increase ListView’s rendering performance. Read carefully here before using it.
7. Avoid wrap_content
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. Use stable Ids
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 getView
11. Use Hierarchy Viewer to analyze the View tree
The tree view should be as shallow and simple as possible. Each view
or viewGroup
in the final ListView item has to be measured and drawn and this takes time.