[英]Items recompose during scrolling list
On my screen I have MyTopItem()
and below list with items.在我的屏幕上,我有
MyTopItem()
和下面的项目列表。 When I'm starting scrolling my list I want to hide MyTopItem()
.当我开始滚动列表时,我想隐藏
MyTopItem()
。
It works fine but scrolling is so laggy.它工作正常,但滚动是如此滞后。 It happens because during scrolling all items in
MyLazyVerticalGridItemsSection()
recompose.发生这种情况是因为在滚动
MyLazyVerticalGridItemsSection()
中的所有项目时重新组合。
How can I avoid recomposing during scrolling?如何避免在滚动期间重新组合?
Column(Modifier.fillMaxSize()) {
val listState = rememberLazyListState()
AnimatedVisibility(
visible = listState.firstVisibleItemScrollOffset < 1,
enter = expandVertically(),
exit = shrinkVertically()
) {
MyTopItem()
}
MyLazyVerticalGridItemsSection(
items = myItems,
listState = listState
)
}
This happens because you're using listState.firstVisibleItemScrollOffset
directly, so each time this value changes, the recomposition is triggered.发生这种情况是因为您直接使用
listState.firstVisibleItemScrollOffset
,因此每次此值更改时,都会触发重组。
In such cases derivedStateOf
should be used - it will only trigger recomposition when the result of the calculation changes::在这种情况下,应该使用
derivedStateOf
- 它只会在计算结果发生变化时触发重组::
val visible by derivedStateOf { listState.firstVisibleItemScrollOffset < 1 }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.