https://kotlinlang.org logo
#compose
Title
# compose
a

Artur Schwarz

07/12/2021, 5:50 PM
Is this a bug? Watch the scrollState.value go from 864 to 0 when the Row gets composed twice (for no obvious reason). This leads to the scroll position not being retained.
Interestingly, this doesn’t happen when performing the first scroll and navigation from Fragment B back to Fragment A
Retaining the Scoll Position when using Texts instead of Images works just fine. Is this a performance issue?
a

Andrey Kulikov

07/12/2021, 7:18 PM
what is ImagePreview? Is it something that asynchronously loading the Images so while they are loading the items heights are 0?
a

Artur Schwarz

07/12/2021, 7:23 PM
@Andrey Kulikov
Copy code
private fun ImagePreview(
    image: File,
    onImageClick: (File) -> Unit
) {
    val onClick = { onImageClick.invoke(image) }

    Card(
        modifier = Modifier
            .clickable { onClick.invoke() }
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Image(
            modifier = Modifier.clickable { onClick.invoke() },
            painter = rememberCoilPainter(image),
            contentDescription = ""
        )
    }
}
Interestingly, the effect shown in the Video does not appear, when i use painterResource(R.drawable.some_drawable) instead of rememberCoilPainter(image)
Regarding asynchonicity: I do load images from a media folder of the App asynchronously
a

Andrey Kulikov

07/12/2021, 7:29 PM
yes, when you use painterResource() the image is loaded synchonously so the Image has the final height straightaway. when you use rememberCoilPainter it is loaded asynchronously, and for the first few frames all of the Images have the 0 height. which means there is 0 sized scrollable container, so the scroll position has to be reset to 0
a

Artur Schwarz

07/12/2021, 7:29 PM
How do i avoid this?
a

Andrey Kulikov

07/12/2021, 7:33 PM
in general when you use
rememberCoilPainter()
it is recommended to predefine the size of the component with some meaningful width and height so it doesn’t jump when you load the images. it is even more important for images inside LazyColumn/LazyRow as there the item sizes are used in order to calculate the amount of needed items. or if you just use the local images from resources use painterResource()
a

Artur Schwarz

07/12/2021, 7:39 PM
This fixed it. Thank you so much @Andrey Kulikov i was actually losing hope and was thinking about falling back to using old RecyclerViews..
a

Andrey Kulikov

07/12/2021, 7:41 PM
yeah, unfortunately there are still a lot of new concepts and things to understand with Compose. but please don’t lose your hope and file bugs if you think there are some aspects we can improve! 🙂
a

Artur Schwarz

07/12/2021, 7:42 PM
Thats true, this is something i would’ve never came up with by myself 😄
2 Views