Как проверить % видимости Composable? ⇐ Android
-
Anonymous
Как проверить % видимости Composable?
I have a Composable that is on a LazyColumn but I'm trying to do the check of visibility out of the LazyListState I used to use this
fun LazyListState.visibleItems(itemVisiblePercentThreshold: Float) = layoutInfo .visibleItemsInfo .filter { visibilityPercent(it) >= itemVisiblePercentThreshold } private fun LazyListState.visibilityPercent(info: LazyListItemInfo): Float { val cutTop = maxOf(0, layoutInfo.viewportStartOffset - info.offset) val cutBottom = maxOf(0, info.offset + info.size - layoutInfo.viewportEndOffset) return maxOf(0f, 100f - (cutTop + cutBottom) * 100f / info.size) } But now I want it inside the Composable since I do not have access to the list, others features use my Composables and it could be inside a LazyList, Surface, whatever, so I'm using onGloballyPositionedto determine if it's visible or not, but I'd like to know if it's at least 30% visible. Any idea?
Explanation
I have a @Composable that I want to provide to other features of my app, so for example :
Feature1 have a LazyColumn that prints its items, but then they want to add my @Composable to the top of the list, so I want to know when my @Composable is visible (at least 30%) on this case if it's at the top normally it should be always 100% when load the list but for example in Feature2 they want to add my @Composable at the middle of their list, so I should know when it start to be visible that's why I need the threshold to send some events.
It wont be always a LazyColumn that's why I don't know if I want to be attached to a LazyListState but if needed I could receive a LazyListState on my @Composable.
Источник: https://stackoverflow.com/questions/772 ... composable
I have a Composable that is on a LazyColumn but I'm trying to do the check of visibility out of the LazyListState I used to use this
fun LazyListState.visibleItems(itemVisiblePercentThreshold: Float) = layoutInfo .visibleItemsInfo .filter { visibilityPercent(it) >= itemVisiblePercentThreshold } private fun LazyListState.visibilityPercent(info: LazyListItemInfo): Float { val cutTop = maxOf(0, layoutInfo.viewportStartOffset - info.offset) val cutBottom = maxOf(0, info.offset + info.size - layoutInfo.viewportEndOffset) return maxOf(0f, 100f - (cutTop + cutBottom) * 100f / info.size) } But now I want it inside the Composable since I do not have access to the list, others features use my Composables and it could be inside a LazyList, Surface, whatever, so I'm using onGloballyPositionedto determine if it's visible or not, but I'd like to know if it's at least 30% visible. Any idea?
Explanation
I have a @Composable that I want to provide to other features of my app, so for example :
Feature1 have a LazyColumn that prints its items, but then they want to add my @Composable to the top of the list, so I want to know when my @Composable is visible (at least 30%) on this case if it's at the top normally it should be always 100% when load the list but for example in Feature2 they want to add my @Composable at the middle of their list, so I should know when it start to be visible that's why I need the threshold to send some events.
It wont be always a LazyColumn that's why I don't know if I want to be attached to a LazyListState but if needed I could receive a LazyListState on my @Composable.
Источник: https://stackoverflow.com/questions/772 ... composable
Мобильная версия