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

Sinan Gunes

03/08/2021, 6:13 PM
about
#AndroidDevChallenge
… My github actions fails on this test:
Copy code
com.example.androiddevchallenge.ExampleInstrumentedTest > sampleTest[test(AVD) - 10] FAILED 
254
	kotlin.UninitializedPropertyAccessException: lateinit property remeasurement has not been initialized
255
	at androidx.compose.foundation.lazy.LazyListState.snapToItemIndexInternal$foundation_release(LazyListState.kt:186)
I haven’t touch that part. Any of you faced similar?
🚫 1
a

Andrey Kulikov

03/08/2021, 6:46 PM
snapToItemIndex
seems to be called before measuring finished. from where do you call it?
s

Sinan Gunes

03/08/2021, 7:00 PM
roughly this is it:
Copy code
Box() {
        val scrollState = rememberLazyListState()
        LazyRow(state = scrollState) {
            items(1000) {
                Text(text = text(it))
            }
        }

        scope.launch {
            scrollState.scrollToItem(index, scroll)
        }
    }
a

Andrey Kulikov

03/08/2021, 8:05 PM
running coroutine right from the composable is never what you want. composable can be recomposed multiple times and then your task will be run again on every recomposition
you basically want to set an initial scroll position, right? the correct way to do it is by passing them as a params to 
rememberLazyListState()
function
s

Shivam Sethi

03/09/2021, 8:03 AM
So how we can achieve the same behaviour where we have to do some suspend task with the preferred way?
b

BenjO

03/09/2021, 8:05 AM
Yes, that's what I have in mind. I don't know how the rule would be enforced, but if the answer is never use launch, maybe there is a thing to automate.
s

Sinan Gunes

03/09/2021, 8:08 AM
@Andrey Kulikov what I want is, when item\scroll changes scroll the view accordingly. not just initially. item\scroll is calculated through a mutable state.
a

Albert Chang

03/09/2021, 8:23 AM
@Shivam Sethi Use
LaunchedEffect
.
s

Sinan Gunes

03/09/2021, 8:24 AM
also used that, did not saw a noticable effect on my use.
a

Andrey Kulikov

03/09/2021, 11:36 AM
Copy code
val scrollState = key(index, scroll) { rememberLazyListState(index, scroll) }
        LazyRow(state = scrollState)
maybe something like this will work for you?
s

Sinan Gunes

03/09/2021, 6:09 PM
hey, this is actually great. and this is how I collected scroll changes as well:
Copy code
LaunchedEffect(scrollState) {
            snapshotFlow { scrollState.firstVisibleItemIndex + (scrollState.firstVisibleItemScrollOffset / cellWidthPx).roundToInt() }
                .distinctUntilChanged()
                .filterNot { counting }
                .onEach {
                    onScrolled(it)
                }
                .collect()
        }
👀 1
amazing… thanks 👍🏻