Nicolai
12/14/2022, 9:42 AMval state = rememberLazyListState()
val visibleIndex: Int by remember {
derivedStateOf {
state.firstVisibleItemIndex
}
}
LazyRow that takes the state
LazyRow(
state = state
)..... bla bla
And then a LaunchedEffect on the visibleIndex
LaunchedEffect(visibleIndex) {
delay(2000)
onMetricViewed(metrics[visibleIndex], visibleIndex)
}
My problem now is that this also runs on the second element in the LazyColumn which is far from visible (maybe around 5%).
I've tried with calculating the visibility of items but couldn't come up with something that worked as I wanted it to.
How can I make sure that
onMetricViewed(metrics[visibleIndex], visibleIndex)
is only called if I can actually see the full height of the element in the LazyRow? 🤔mattinger
12/14/2022, 4:47 PMNicolai
12/14/2022, 8:34 PMmattinger
12/14/2022, 11:37 PMmattinger
12/14/2022, 11:38 PMmattinger
12/14/2022, 11:57 PM@Composable
@Preview
fun x() {
Timber.uprootAll()
Timber.plant(Timber.DebugTree())
MaterialTheme {
Surface(modifier = Modifier.fillMaxSize()) {
val columnState = rememberLazyListState()
var columnSize by remember { mutableStateOf(IntSize(0, 0)) }
LazyColumn(
modifier = Modifier
.fillMaxSize()
.onGloballyPositioned {
columnSize = it.size
},
state = columnState
) {
(1..100).forEach {
item {
Text("$it")
}
}
}
val layoutInfoFlow = snapshotFlow { columnState.layoutInfo }
LaunchedEffect(key1 = true) {
layoutInfoFlow.collect {
it.visibleItemsInfo.forEach {
val isFullyVisible = (it.offset + it.size) <= columnSize.height
Timber.tag("Visibility").d(
"Item at ${it.index} fullyVisible: ${isFullyVisible}"
)
}
}
}
}
}
}
mattinger
12/14/2022, 11:58 PM