[英]How to check if the items in LazyColumn is too few for the height of the screen?
In such cases you should use the lazy list state: it contains information about the visible elements, you need to check that all of them are fully visible.在这种情况下,您应该使用惰性列表状态:它包含有关可见元素的信息,您需要检查它们是否完全可见。
When you need to read any state continuously and produce some value depending on this state, you should use derivedStateOf
to prevent redundant recompositions.当您需要连续读取任何状态并根据该状态产生一些值时,您应该使用derivedStateOf
来防止冗余重组。
val state = rememberLazyListState()
val canBeScrolled by remember {
derivedStateOf {
val layoutInfo = state.layoutInfo
val visibleItemsInfo = layoutInfo.visibleItemsInfo
if (layoutInfo.totalItemsCount == 0) {
false
} else {
val firstVisibleItem = visibleItemsInfo.first()
val lastVisibleItem = visibleItemsInfo.last()
val viewportHeight = layoutInfo.viewportEndOffset + layoutInfo.viewportStartOffset)
!(firstVisibleItem.index == 0 &&
firstVisibleItem.offset == 0 &&
lastVisibleItem.index + 1 == layoutInfo.totalItemsCount &&
lastVisibleItem.offset + lastVisibleItem.size <= viewportHeight)
}
}
}
LazyColumn(state = state) {
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.