简体   繁体   English

如何检查 LazyColumn 中的项目对于屏幕高度是否太少?

[英]How to check if the items in LazyColumn is too few for the height of the screen?

Since there is no scrollbar for LazyColumn, I tried this solution.由于 LazyColumn 没有滚动条,我尝试了这个解决方案。

Is there a way for me to check if items are too few for the list to scroll because the scrollbar is still visible and I want to hide it if possible?有没有办法让我检查项目是否太少而无法滚动列表,因为滚动条仍然可见并且我想尽可能隐藏它?

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.

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