[英]Jetpack Compose: How to listen column currently visible items like lazycolumn?
I want to use Column instead of LazyColumn, and make it scrollable by Modifier.verticalScroll(scrollState)
, is there any way to get currently visible items key that users see on the screen?我想使用 Column 而不是 LazyColumn,并通过Modifier.verticalScroll(scrollState)
使其可滚动,有什么方法可以获取用户在屏幕上看到的当前可见项键吗?
I'm not positive that I fully understand, so this may be off base but here's some code that keeps track of the position of the items within a scrollable column and updates a text whenever the first visible item changes.我不确定我是否完全理解,所以这可能是离题的,但这里有一些代码可以跟踪可滚动列中项目的 position,并在第一个可见项目发生变化时更新文本。 Performance seems okay, at least for a list of items that would be reasonable for a non-lazy column.性能似乎还可以,至少对于对于非惰性列来说合理的项目列表而言是这样。
@Composable
fun Example() {
val positions = remember { items.map { 0f }.toMutableStateList() }
val ss = rememberScrollState()
val firstVisibleItem by remember {
derivedStateOf {
positions.indexOfLast {
it <= ss.value
}
}
}
Column(Modifier.fillMaxSize()) {
Text(
items.getOrElse(firstVisibleItem) { "None "},
Modifier
.fillMaxWidth()
.background(Color.LightGray)
.padding(16.dp)
)
Column(
Modifier
.verticalScroll(ss)
.fillMaxSize()
.padding(16.dp)
) {
items.forEachIndexed { index, item ->
Text(
text = item,
modifier = Modifier
.height(100.dp)
.onGloballyPositioned {
positions[index] = it.positionInParent().y
}
)
}
}
}
}
As an aside, I'd be surprised if a normal scrollable column had better performance than a lazy one unless the item count is very small.顺便说一句,如果一个普通的可滚动列比一个惰性的列有更好的性能,我会感到惊讶,除非项目计数非常小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.