Android ExpandableListView animation expansion effect and Performance Optimization Using traceview, androidtraceview
Solution Github pull request link: https://github.com/idunnololz/AnimatedExpandableListView/pull/30
Android native provides and expands the group's ListView: ExpandableListView. However, compared with the UITableView provided by the native on iOS, its UI capabilities are insufficient. For example, it does not support native animation expansion and collapse.
In the open source code community, we can find several animation solutions added for Android's ExpandableListView. Among them, the AnimatedExpandableListView of idunnololz is one of the good solutions.
Https://github.com/idunnololz/AnimatedExpandableListView ). Its advantages: the performance is good, the source code is provided rather than the library (this is important), and the annotations are clear.
However, there is no end to performance optimization. When the child view (childView) in the group becomes complex, or the parent structure of the ListView is complex, such as embedded with other LinearLayout, FrameLayout, or ScrollView, in addition, when parent uses the custom override onMeasure () method, the efficiency of generating childView will greatly affect the application performance.
The key to the rational use of AnimatedExpandableListView is the implementation of AnimatedExpandableListView # getRealChildView (), which is the responsibility of application development. In actual projects, by optimizing getRealChildView (), the animation effect start time is reduced from 680 ms to ms (expand a group containing five sub-projects ). The problem locating and solutions are basically analyzed using the method tracing method (Android. OS. Debug. startMethodTraceing) provided by android.
The getRealChildView () implementation before optimization requires a large number of view initialization because there is no convertView available. In fact, the childView generated during the animation rendering stage can be completely reused, convertView in a timely manner. As shown in the following traceview profile, getChildView () consumes more than one second before optimization.
Optimized Performance:
How is this done? This requires us to study the principle of animation expansion, that is, the longest time-consuming action in getChildView. First, eliminate the impact of other factors and focus on the use of AnimatedExpandableList. We will use the Example native on GitHub for analysis: this is the case where we expand the grouping of five sub-projects, pay attention to the view generation of the five sub-groups, LayoutInflater. inflate is executed 10 times, which is twice. Inflate is quite time-consuming. Is there a way to reduce this part of work consumption?
You can use the LRU cache recommended by Android to save childView. For more information about LruCache, see Android reference documents and training. Note that childView needs to be generated again when the dataSet changes, rather than obtained in the cache. The method used here is to judge the type of childView. In your own project, you need to carefully consider how dataSet updates the cache based on your needs. The effect is as follows: the number of inflate operations is reduced to 5, so that one operation is not wasted. The consumption time is reduced from ms to 80 ms.
Write an ExpandableListView to achieve click Effect
Configure listSector
Android ExpandableListView
Xml:
Android: cacheColorHint = "#00000000"
Java code:
ListView. setCacheColorHint (0x00000000)