Nthily
06/19/2024, 12:33 PMLazyColumn
, and its last item is at the bottom of the current screen. The content is dynamically increasing, causing the LazyColumn
to automatically scroll to the bottom. However, I do not want it to scroll, I want it to remain in its original position. Is there a good way to achieve this?Nthily
06/19/2024, 1:04 PMAndrey Kulikov
06/19/2024, 1:07 PMNthily
06/19/2024, 1:17 PMNthily
06/19/2024, 1:18 PMAndrey Kulikov
06/19/2024, 1:20 PMAndrey Kulikov
06/19/2024, 1:21 PMAndrey Kulikov
06/19/2024, 1:23 PMAndrey Kulikov
06/19/2024, 1:24 PMNthily
06/19/2024, 1:28 PMreverseLayout = false,
reference:
https://kotlinlang.slack.com/archives/CJLTWPH7S/p1715074009880069?thread_ts=1715063833.799509&cid=CJLTWPH7S
because of this issue, that's why I use reversedLayout = true
🤔
but yes, I also think that reversedLayout is not needed in my case, but I need imePadding because it allows the LazyColumn item to move with the ime when it is openedAndrey Kulikov
06/19/2024, 1:45 PMAndrey Kulikov
06/19/2024, 1:46 PMAndrey Kulikov
06/19/2024, 1:48 PMNthily
06/19/2024, 2:02 PMNthily
06/19/2024, 2:02 PMreverseLayout
, this API scrolls the current list to the bottom of the "last item". However, what I need is for the list to remain fixed at the position I scroll to, and not continue scrolling.
But it seems like you understand the behavior from the first video?
However, I also need the effect shown in the second video, where the automatic scrolling stops after I manually scroll the listNthily
06/19/2024, 2:14 PMAndrey Kulikov
06/19/2024, 2:26 PMNthily
06/19/2024, 2:27 PMNthily
06/19/2024, 2:35 PMreverseLayout
is either false
or `true`:
1. reverseLayout = false
In this scenario:
Column {
LazyColumn(...)
TextField(Modifier.imePadding()) // The items within the LazyColumn cannot move together with the IME (as previously discussed)
}
In this case, we can use:
if (!state.canScrollForward) state.requestScrollToItem(lastIndex)
to achieve automatic scrolling and to cancel automatic scrolling (e.g., after some distance has been scrolled).
2. reverseLayout = true
In this scenario, the TextField
can sync well with the LazyColumn
during the IME opened/closed, but the issue is that after scrolling some distance, we cannot cancel automatic scrollingNthily
06/19/2024, 2:37 PMreverseLayout
, so that it doesn't automatically scroll to the bottom. Instead, I can manually scroll the list when the item height changes. However, this approach would cause the imePadding
effect to become ineffective. So, I'm not quite sure how to proceed from here... 🥲Andrey Kulikov
06/19/2024, 2:55 PMAndrey Kulikov
06/19/2024, 3:00 PMAlbert Chang
06/19/2024, 3:53 PMLazyListState.scrollBy()
with that value?
Maybe something like this (not verified):
val listState = rememberLazyListState()
val imeInsets = WindowInsets.ime
val density = LocalDensity.current
LaunchedEffect(listState, imeInsets, density) {
var prev = imeInsets.getBottom(density)
snapshotFlow { imeInsets.getBottom(density) }.collect {
val delta = it - prev
if (delta != 0) {
prev = it
listState.scrollBy(delta.toFloat())
}
}
}
Andrey Kulikov
06/19/2024, 3:56 PMNthily
06/19/2024, 4:17 PMAndrey Kulikov
06/19/2024, 4:23 PMStylianos Gakis
07/16/2024, 4:57 PMStylianos Gakis
07/16/2024, 5:06 PMif (!lazyListState.canScrollBackward && lazyListState.layoutInfo.visibleItemsInfo.isNotEmpty()) {
As a hack to "skip" the first frame where there are no items in the list, which I assumed is what made !lazyListState.canScrollBackward
evaluate to true, and it seems to work fine for my super limited testing right now.
Would still love to hear your thoughts on this though