Xigong93/ExpandableRecyclerView

和 ExpandableListView 相比在性能上有什么优势吗?

REBOOTERS opened this issue · 2 comments

有一些场景需要这种控件,不知道和系统自带的相比是否有优化。

ExpandableRecyclerView和ExpandableListView相比的优点:

  • 支持流畅的关闭和展开动画
  • RecyclerView相比ListView的优势,比如item局部刷新
  • StickyHeader是支持局部刷新的

ExpandableListView

默认是没有展开和关闭动画,第三方实现有了动画,大部分都是通过设置View.height 来实现动画效果的。这种方式实现的动画比较卡,每一帧都需要测量,布局和绘制,如果遇到Item比较复杂,会出现动画卡顿或抖动。
ExpandableRecyclerView
动画是通过属性动画实现的,每一帧只有部分重绘,不会重新测量和布局,动画更加流畅。

性能的测量方式

比如对比https://github.com/idunnololz/AnimatedExpandableListView 这个库,把ItemView放一个HeavyTextView ,打开手机的HWUI渲染模式,通过比较AnimatedExpandableListView ,ExpandableRecyclerView,展开和关闭动画,就能发现ExpandableRecyclerView在动画的执行过程中非常流畅。

/**
 * 测量比较耗时的TextView,用来模拟性能问题
 */ 
class HeavyTextView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextView(context, attrs, defStyleAttr) {

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        SystemClock.sleep(3);// 模拟测量耗时
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }
}

😯,厉害了,回头替换一波试试。