ursus
01/13/2023, 8:03 PMlazyListState.layoutInfo.visibleItemsInfo
, it’s only forward-looking like
///////// first visible index=3 o=448
item= index=3 offset=-448 height=589
item= index=4 offset=141 height=571
item= index=5 offset=712 height=153
granted, it firstVisibleItemIndex
starts from 0,
so, I could cache these height values, to then calculate actual scroll position, which is what I’m after
Question is, where would I cache such values?
something to the effect of
val info = listState.layoutInfo.visibleItemsInfo
val cache = Array(size = info.size) { 0 }
info.forEach {
cache[it.index] = it.size
}
would a simple remember
be enough?Alex Vanyo
01/13/2023, 8:25 PMursus
01/13/2023, 8:29 PMursus
01/13/2023, 8:33 PMsearchBar.translationY = scrollPosition.coerceIn(0, 500)
for example
the issue is, if the list has say very short items, then I’d only see listState.firstVisibleItemIndex
0..50, then again 0..50, etc. not 0..1000 (or whatever the visible total height is)
tldr; firstVisibleItemIndex gets reset, which would cause my translationY to reset to 0 and it would “jump”, which is not goodursus
01/13/2023, 8:35 PMAlex Vanyo
01/13/2023, 8:41 PMI think I only need it working at least when it starts from 0What happens if you scroll the list partway, and then rotate? You then could be starting from the middle of the list, with new sizes.
and the 1000 I could live withMaybe you could get away with having the first set of items be non-lazy? So the first “item” from the lazy list perspective is actually a few items in a
Column
.
So something like:
item {
Column {
// Items 1-4
}
}
items {
// Items 5-the rest
}
You’d probably have to tune the amount of actual items you render at the top in your “special” item. If you don’t do enough, you’d have the same issue now, and if you do too many, you’d be defeating the point of a LazyColumn
.Alex Vanyo
01/13/2023, 8:43 PMursus
01/13/2023, 8:44 PMlistState.visibleInfo
then I see the last offset being 1519, which plus item height, is like 1.5 “screens” of height; which should be fine, so, 0-1519 you think is impossible? I mean the cache I proposed would workursus
01/13/2023, 8:45 PMursus
01/13/2023, 8:45 PMursus
01/13/2023, 8:52 PMursus
01/13/2023, 8:52 PMursus
01/13/2023, 8:55 PMursus
01/13/2023, 8:58 PMAlex Vanyo
01/13/2023, 9:00 PMexitUntilCollapsedScrollBehavior
, which I think uses the nested scrolling as its implementationursus
01/13/2023, 9:01 PMursus
01/13/2023, 9:02 PMfirstVisibleItemOffset
in the orange google music example is it would reset to 0 when the “Its Tuesday+Play music for” item would be scrolled invisibleursus
01/13/2023, 9:03 PMAlex Vanyo
01/13/2023, 9:10 PMursus
01/13/2023, 9:11 PMursus
01/13/2023, 9:19 PMD/Default: item= index=0 offset=-41 height=222
D/Default: item= index=1 offset=181 height=563
D/Default: item= index=2 offset=744 height=766
D/Default: item= index=3 offset=1510 height=589
and my use case would be fine with having scroll position within 0..1510
then, I’d just follow the offset of the index=3
and the question is, where would I cache the initial offset of the 1510, so I then can substract it and get the actual scroll position?ursus
01/13/2023, 9:32 PMval y = if (listState.layoutInfo.visibleItemsInfo.isNotEmpty()) {
val initial = remember { listState.indexOffset(3) }
val current = listState.indexOffset(3)
initial - current
} else {
0
}
private fun LazyListState.indexOffset(index: Int): Int {
return layoutInfo.visibleItemsInfo.firstOrNull { it.index == index }?.offset ?: 0
}
this sort of works, see any problems with it?Alex Vanyo
01/13/2023, 9:36 PM3
isn’t available, like if you’re scrolled further down the list.
For performance concerns, this is one of the primary use cases for nested scrolling so I wouldn’t be concerned there, and I was just trying out the sample of the collapsing top app bar and didn’t see the 2 flings issue.ursus
01/13/2023, 9:37 PMursus
01/13/2023, 10:39 PMAlex Vanyo
01/13/2023, 10:46 PMLargeTopAppBar
have an example using the `exitUntilCollapsedScrollBehavior`:
https://developer.android.com/reference/kotlin/androidx/compose/material3/package-summary#LargeTopAppBar(kotlin.Funct[…]al3.TopAppBarScrollBehavior)ursus
01/13/2023, 10:47 PMursus
01/13/2023, 10:47 PM