[英]Jetpack Compose LazyVerticalGrid stuttering
I'm migrating my app and for the grid view I used LazyVerticalGrid that loads images using coilPainter but the scrolling stutters a lot and it feels laggy.我正在迁移我的应用程序,对于网格视图,我使用了 LazyVerticalGrid,它使用 coilPainter 加载图像,但滚动时断断续续很多,而且感觉滞后。 Does anyone have a solution or a better implementation for this?
有没有人对此有解决方案或更好的实现?
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,
)
}
}
}
}
}
Update更新
Trying to move rememberCoilPainter
above Image
like val painter = rememberCoilPainter(photo.url)
and use the painter
inside the Image
did not work试图移动
rememberCoilPainter
以上Image
等val painter = rememberCoilPainter(photo.url)
并使用painter
内的Image
没有工作
items
takes an additional parameter key
, assign this parameter to a unique value like the index of each photo of your list, If you don't know how does that relate to scrolling jank? items
需要一个额外的参数key
,将此参数分配给一个唯一值,例如列表中每张照片的索引,如果您不知道这与滚动卡顿有什么关系? Or what the heck am I talking about?或者我到底在说什么? check out the following documentation for a detailed explanation:
查看以下文档以获取详细说明:
https://developer.android.com/jetpack/compose/lifecycle#add-info-smart-recomposition https://developer.android.com/jetpack/compose/lifecycle#add-info-smart-recomposition
After updating my compose image loading library to coil-compose I was forced to set a size to either the request builder or the image itself.将我的 compose 图像加载库更新为线圈组合后,我被迫将大小设置为请求构建器或图像本身。 The problem of the stuttering was caused by allowing the photo to load the original size and thus recomposing was done multiple times and loading the image had to wait for the picture to load.
卡顿的问题是因为允许照片加载原始尺寸,因此重新构图进行了多次,加载图像必须等待图片加载。 By setting e fixed height to the Image fixed the issue because the Images() were created and the recomposition was made only for the picture inside not for the Image itself with different height.
通过将 e固定高度设置为 Image 解决了该问题,因为创建了 Images() 并且仅对内部图片进行了重新组合,而不是针对具有不同高度的 Image 本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.