I want to have the total scroll position in px in LazyColumn. The column itself has a ScrollState which has a value (Described in the document as the current scroll position value in pixels), But LazyColumn ScrollState hasn't this field, I want the exact equivalent of Column ScrollState scroll position value for LazyColumn, How I can achieve that with the help of LazyScrollState?
4 Answers
You can get it by firstVisibleItemScrollOffset
.
('androidx.activity:activity-compose:1.3.1')
val listState = rememberLazyListState()
val itemHeight = with(LocalDensity.current) { 80.dp.toPx() } // Your item height
val scrollPos = listState.firstVisibleItemIndex * itemHeight + listState.firstVisibleItemScrollOffset
Text(text = "scroll: $scrollPos")
LazyColumn(state = listState) {
// Your items here
}
Also, you can set the scroll position to listState
, like this:
LaunchedEffect(key1 = "Key") {
delay(1000) // To show scroll explicitly, set the delay
listState.scrollBy(itemHeight * 2)
}

- 3,383
- 3
- 34
- 60
-
As the variable itself is saying, this is the FIRST VISIBLE item index. It resets to zero everytime an item goes out of the viewport. – Amir Jan 16 '23 at 07:03
-
@Amir, can you see "listState.firstVisibleItemIndex * itemHeight"? Of course, it will show the scroll offset of the first item, with the (index + height). That means it will show the current scroll position of the item + the previous scroll. – Stanley Ko Jan 16 '23 at 10:07
-
What if the list items are not the same size? – Amir Jan 16 '23 at 11:53
-
@Amir in that case, you should add all the heights of items. I couldn't found any solution for that. – Stanley Ko Jan 16 '23 at 12:10
The scroll amount can be obtained, no calculation required, in the items themselves, by attaching an onGloballyPosition{ it.positionInParent()}
modifier to one or more items.
Then, the items can do what they need to do with their own scroll position, such as offsetting some screen-drawing y coordinate.
Or, if you need the scroll offset in the parent LazyColumn, you could have one item (perhaps an item with zero height, though I've not tested that) at the very top of your list of items, that reports back its position to the parent (perhaps by updating a mutableState that was passed to the item by the parent) whenever it moves.
I had the same need and onGloballyPosition{ it.positionInParent()}
addressed it very nicely.

- 5,581
- 6
- 29
- 46
-
This is not working. As soon as the item gets out of the viewport it won't be available anymore. Unless your LazyColumn doesn't recycle items which is not good for performance. – Amir Jan 16 '23 at 07:01
It is not possible to know the exact scroll position, but we can try!
@Composable
fun rememberCurrentOffset(state: LazyListState): State<Int> {
val position = remember { derivedStateOf { state.firstVisibleItemIndex } }
val itemOffset = remember { derivedStateOf { state.firstVisibleItemScrollOffset } }
val lastPosition = rememberPrevious(position.value)
val lastItemOffset = rememberPrevious(itemOffset.value)
val currentOffset = remember { mutableStateOf(0) }
LaunchedEffect(position.value, itemOffset.value) {
if (lastPosition == null || position.value == 0) {
currentOffset.value = itemOffset.value
} else if (lastPosition == position.value) {
currentOffset.value += (itemOffset.value - (lastItemOffset ?: 0))
} else if (lastPosition > position.value) {
currentOffset.value -= (lastItemOffset ?: 0)
} else { // lastPosition.value < position.value
currentOffset.value += itemOffset.value
}
}
return currentOffset
}
Using the rememberPrevious
from Get previous value of state in Composable - Jetpack Compose solution also.
How to use
val scroll = rememberLazyListState()
val offset = rememberCurrentOffset(scroll)
// do any thing with the offset state
LazyColumn(
state = scroll,
modifier = Modifier.fillMaxSize()
) {
....
}

- 575
- 5
- 9
The reason it exists in Column is because it knows about all the items (rows) when it's being generated.
But LazyColumn only displays a portion of the total views at any given moment, and when you scroll, it continuously re-generates the views on the screen (plus a couple above and/or below the visible area), so it never actually calculates the total height of all the rows. If you wanted to get the scroll position, you would have to manually calculate it. If the height of each row is the same, it'll work pretty well. But if the heights are different, then you won't be able to get the exact scroll position (your calculations will fluctuate depending on the height of the rows that are currently displayed). But here's what you have to do to calculate it yourself:
Calculate the total size of all the items
val totalItems = lazyListState.layoutInfo.totalItemsCount
val itemLengthInPx = lazyListState.layoutInfo.visibleItemsInfo.firstOrNull()?.size ?: 0
val totalLengthInPx = totalItems * itemLengthInPx
Calculate the current scroll position
val scrollPos = (lazyListState.firstVisibleItemIndex * itemLengthInPx) / totalLengthInPx
But as I mentioned earlier, this calculation depends on the height of each item (itemLengthInPx
), and it works perfectly if it's the same for all the views. But you can see how it'll fluctuate if each view has a different height

- 6,461
- 10
- 47
- 84
-
2This is not exactly like "Scroll Position", Scroll position in the Column will change every time we scroll the list and the offset of the first item changes a little bit, But in this way, this value would be updated if the index of the first visible item change (firstVisibleItemIndex) – Hamidreza Sahraei Aug 13 '22 at 07:11
-
You're describing the scroll position in a Column. It's completely different for a LaxyColumn (for all the reasons I explained in my answer). The whole point is that only a few items are being displayed at any give time, and you need to calculate where those rows are in relation to the full list of items. So, you definitely need to track the firstVisibleItemIndex -- it tells you what "section" is being displayed – user496854 Aug 13 '22 at 12:52
-