简体   繁体   English

LazyColumn Jetpack Compose 中的当前滚动 position 值(以像素为单位)

[英]Current scroll position value in pixels in LazyColumn Jetpack Compose

I want to have the total scroll position in px in LazyColumn.我想要在 LazyColumn 中以 px 为单位的总滚动 position。 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?该列本身有一个 ScrollState,它有一个值(在文档中描述为当前滚动 position 值(以像素为单位)),但是 LazyColumn ScrollState 没有这个字段,我想要完全等同于 LazyColumn 的 Column ScrollState 滚动 position 值,我如何可以借助 LazyScrollState 实现吗?

The reason it exists in Column is because it knows about all the items (rows) when it's being generated.它存在于 Column 中的原因是因为它在生成时知道所有项目(行)。

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.但是 LazyColumn 在任何给定时刻仅显示总视图的一部分,并且当您滚动时,它会不断地在屏幕上重新生成视图(加上可见区域上方和/或下方的一对),因此它从不实际计算所有行的总高度。 If you wanted to get the scroll position, you would have to manually calculate it.如果您想获得卷轴 position,则必须手动计算。 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).但是如果高度不同,那么您将无法获得精确的滚动 position(您的计算将根据当前显示的行的高度而波动)。 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计算当前滚动 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.但正如我之前提到的,这个计算取决于每个项目的高度( itemLengthInPx ),如果所有视图都相同,它就可以完美地工作。 But you can see how it'll fluctuate if each view has a different height但是你可以看到如果每个视图都有不同的高度,它会如何波动

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.通过将onGloballyPosition{ it.positionInParent()}修饰符附加到一个或多个项目,可以在项目本身中获得滚动量,无需计算。

Then, the items can do what they need to do with their own scroll position, such as offsetting some screen-drawing y coordinate.然后,项目可以用自己的滚动position来做他们需要做的事情,例如偏移一些屏幕绘图的y坐标。

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.或者,如果您需要父 LazyColumn 中的滚动偏移量,您可以在项目列表的最顶部有一个项目(可能是高度为零的项目,尽管我没有测试过),它会将其 position 报告给每当它移动时,父级(可能通过更新由父级传递给项目的 mutableState)。

I had the same need and onGloballyPosition{ it.positionInParent()} addressed it very nicely.我有同样的需求, onGloballyPosition{ it.positionInParent()}很好地解决了这个问题。

You can get it by firstVisibleItemScrollOffset .您可以通过firstVisibleItemScrollOffset获取它。
('androidx.activity:activity-compose:1.3.1') ('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:此外,您可以将滚动条 position 设置为listState ,如下所示:

LaunchedEffect(key1 = "Key") {
    delay(1000) // To show scroll explicitly, set the delay
    listState.scrollBy(itemHeight * 2)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM