rajesh
08/22/2021, 10:13 AMindex?.let {}
block. What are the alternatives?rajesh
08/22/2021, 10:13 AM@Composable
fun DetailScreen(
indexToScroll: Int? = null
) {
val coroutineScope = rememberCoroutineScope()
val listState = rememberLazyListState()
LazyColumn(
state = listState
) {
// LazyColumn Content
indexToScroll?.let { index ->
// Can't use LaunchedEffect here
Log.d(TAG, "scrolling to $index")
coroutineScope.launch {
listState.scrollToItem(index = index)
}
}
}
}
Oleksandr Balan
08/22/2021, 11:45 AMDetailScreen
composable, so I believe you could have your LaunchedEffect
there. Right before LazyColumn
composable usage:
fun DetailScreen(...) {
val listState = ...
LaunchedEffect(indexToScroll) { ... }
LazyColumn { ... }
}
rajesh
08/22/2021, 12:02 PMval listState = rememberLazyListState()
LaunchedEffect(key1 = indexToScroll) {
indexToScroll?.let { index ->
Log.d(TAG, "scrolling to $index")
coroutineScope.launch {
listState.scrollToItem(index = index)
}
}
}
LazyColumn() { ... }
Chris Miller
08/22/2021, 12:39 PMOleksandr Balan
08/22/2021, 12:45 PMChris Miller
08/22/2021, 12:48 PMChris Miller
08/22/2021, 12:58 PMfun ItemList(model: MyViewModel) {
val selectedIndex by remember { model.selectedItem }
val state = rememberLazyListState(selectedIndex)
LazyColumn(state = state) { ... }
LaunchedEffect(selectedIndex) {
if (!state.isVisible(selectedIndex)) {
state.animateScrollToItem(selectedIndex)
}
}
}
rajesh
08/22/2021, 1:07 PMLaunchedEffect
below LazyColumn
but that didn't worked , too
val listState = rememberLazyListState()
LazyColumn() { ... }
LaunchedEffect(key1 = indexToScroll) {
indexToScroll?.let { index ->
Log.d(TAG, "scrolling to $index")
coroutineScope.launch {
listState.scrollToItem(index = index)
}
}
}