简体   繁体   English

Jetpack Compose LazyVerticalGrid 口吃

[英]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以上Imageval 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.

相关问题 LazyVerticalGrid 用于在 jetpack compose 中分页项目 - LazyVerticalGrid for paging items in jetpack compose 如何使用带有 LazyVerticalGrid 的 jetpack 组合分页 - How to use jetpack compose paging with LazyVerticalGrid Jetpack Compose:如何在 LazyVerticalGrid 中将项目居中? - Jetpack Compose: How to center an item in LazyVerticalGrid? 如何在 jetpack compose 中使用 lazy column 和 lazyverticalgrid? - How can I use lazy column and lazyverticalgrid in jetpack compose? Jetpack 组合 LazyVerticalGrid 项目抛出 java.lang.IllegalStateException - Jetpack compose LazyVerticalGrid items throws java.lang.IllegalStateException Jetpack Compose 的 LazyVerticalGrid 是否有跨度策略 - Does Jetpack Compose's LazyVerticalGrid have span strategy Jetpack Compose:如何将 LazyVerticalGrid 放入可滚动列中? - Jetpack Compose: How to put a LazyVerticalGrid inside a scrollable Column? 如何在 jetpack compose 中向 LazyVerticalGrid 添加一个 stickyHeader,例如 LazyColumn? - How can I add a stickyHeader to LazyVerticalGrid like LazyColumn in jetpack compose? 在 Jetpack Compose 中使用 HorizontalPager + LazyVerticalGrid 时性能不佳 - Performance is bad when using HorizontalPager + LazyVerticalGrid in Jetpack Compose 有没有办法在 Jetpack Compose 的垂直可滚动视图中嵌套 LazyVerticalGrid - Is there a way to nest a LazyVerticalGrid iniside a vertical scrollable view in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM