Jetpack Compose LazyVerticalGrid 卡顿
Jetpack Compose LazyVerticalGrid stuttering
我正在迁移我的应用程序,对于网格视图,我使用了 LazyVerticalGrid,它使用 coilPainter 加载图像,但滚动时断断续续很多,而且感觉很慢。有人对此有解决方案或更好的实现吗?
val photos: List<Photo>? by photosViewModel.photosLiveData.observeAsState(null)
Surface(modifier = Modifier.fillMaxSize()) {
Box(
modifier = Modifier.fillMaxSize()
) {
LazyVerticalGrid(
cells = GridCells.Fixed(3)
) {
photos?.let { photosList ->
items(photosList) { photo ->
Image(
modifier = Modifier.padding(2.dp).clickable {
photosViewModel.onPhotoClicked(photo)
},
painter = rememberCoilPainter(photo.url),
contentDescription = stringResource(R.string.cd_details_photo),
contentScale = ContentScale.Fit,
alignment = Alignment.Center,
)
}
}
}
}
}
更新
尝试将 rememberCoilPainter
移动到 Image
上方,如 val painter = rememberCoilPainter(photo.url)
并使用 Image
内的 painter
无效
items
采用附加参数 key
,将此参数分配给唯一值,例如列表中每张照片的索引,如果您不知道这与滚动卡顿有何关系?或者我到底在说什么?查看以下文档以获得详细说明:
https://developer.android.com/jetpack/compose/lifecycle#add-info-smart-recomposition
将我的合成图像加载库更新为 coil-compose 后,我被迫为请求生成器或图像本身设置大小。卡顿的问题是由于允许照片加载原始尺寸,因此重新构图进行了多次,加载图像必须等待图片加载。通过将 e fixed height 设置为 Image 解决了这个问题,因为创建了 Images() 并且仅针对内部图片进行了重组,而不是针对具有不同高度的 Image 本身。
我正在迁移我的应用程序,对于网格视图,我使用了 LazyVerticalGrid,它使用 coilPainter 加载图像,但滚动时断断续续很多,而且感觉很慢。有人对此有解决方案或更好的实现吗?
val photos: List<Photo>? by photosViewModel.photosLiveData.observeAsState(null)
Surface(modifier = Modifier.fillMaxSize()) {
Box(
modifier = Modifier.fillMaxSize()
) {
LazyVerticalGrid(
cells = GridCells.Fixed(3)
) {
photos?.let { photosList ->
items(photosList) { photo ->
Image(
modifier = Modifier.padding(2.dp).clickable {
photosViewModel.onPhotoClicked(photo)
},
painter = rememberCoilPainter(photo.url),
contentDescription = stringResource(R.string.cd_details_photo),
contentScale = ContentScale.Fit,
alignment = Alignment.Center,
)
}
}
}
}
}
更新
尝试将 rememberCoilPainter
移动到 Image
上方,如 val painter = rememberCoilPainter(photo.url)
并使用 Image
内的 painter
无效
items
采用附加参数 key
,将此参数分配给唯一值,例如列表中每张照片的索引,如果您不知道这与滚动卡顿有何关系?或者我到底在说什么?查看以下文档以获得详细说明:
https://developer.android.com/jetpack/compose/lifecycle#add-info-smart-recomposition
将我的合成图像加载库更新为 coil-compose 后,我被迫为请求生成器或图像本身设置大小。卡顿的问题是由于允许照片加载原始尺寸,因此重新构图进行了多次,加载图像必须等待图片加载。通过将 e fixed height 设置为 Image 解决了这个问题,因为创建了 Images() 并且仅针对内部图片进行了重组,而不是针对具有不同高度的 Image 本身。