在 Jetpack Compose 中使用 Horizo​​ntalPager + LazyVerticalGrid 时性能较差

The*_*awd 6 android kotlin android-jetpack-compose

我设置了一个非常小的项目用于测试目的,并且在滑动水平页面时看到很多卡顿帧: https: //github.com/DaFaack/ComposePerformanceTest

我在启用 R8 的发布模式下构建。我究竟做错了什么?

当我记录跟踪并在 perfetto 中查看它时,它显示了很多卡顿: 在此输入图像描述

使用 ViewPager + RecyclerView 在 XML 中进行相同的设置运行顺利

我尝试生成基线配置文件,但没有看到任何差异,仍然是卡顿的帧

Sub*_*fly -3

而不是使用:

LazyVerticalGrid(columns = GridCells.Fixed(5), content = {
        items(40) {
            ImageItem(imageUrl, name)
        }
    })
Run Code Online (Sandbox Code Playgroud)

使用:

(0..40).forEach { it ->
    item(key: /*Create an constant key for each item*/) {
        ImageItem(imageUrl, name)
    }
}
Run Code Online (Sandbox Code Playgroud)

我在某处读到,在列表上使用 foreach 以及自定义键可以大大提高性能。此外,请尝试在任何可能的情况下使用 fastForEach。