Created
December 12, 2024 17:39
-
-
Save acious/4b310b62502e9040e9f6ed5800f20400 to your computer and use it in GitHub Desktop.
Android Compose - Composable(View) onExpose Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @SuppressLint("CoroutineCreationDuringComposition", "UnnecessaryComposedModifier") | |
| @Composable | |
| fun Modifier.onExpose( | |
| onVisibilityChanged: (Boolean) -> Unit, | |
| exposeCriteriaSizeRate: Float = 1f, | |
| ): Modifier = composed { | |
| this.onGloballyPositioned { layoutCoordinates -> | |
| val boundsInWindow = layoutCoordinates.boundsInWindow() | |
| val totalArea = layoutCoordinates.size.run { width * height }.toFloat() | |
| val visibleArea = boundsInWindow.width * boundsInWindow.height | |
| val visibilityRate = visibleArea / totalArea | |
| onVisibilityChanged(visibilityRate >= exposeCriteriaSizeRate) | |
| } | |
| } | |
| // Usage | |
| Row( | |
| modifier = | |
| Modifier.onExpose( | |
| onVisibilityChanged = { isVisible -> viewState.value = isVisible }, | |
| exposeCriteriaSizeRate = 0.5f, | |
| ), | |
| ) { | |
| Text( | |
| text = "이 뷰가 절반정도 보여지면 onVisibilityChanged 콜백 트리거링", | |
| style = PrexTheme.typeScale.textXXS, | |
| color = PrexTheme.fdsColor.contentAccentInfo, | |
| maxLines = 1, | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment