Last active
July 6, 2026 08:19
-
-
Save benigumocom/f9525ad7833ab00e385343cf5f893e76 to your computer and use it in GitHub Desktop.
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
| @Composable | |
| fun ScrollAwareLazyColumn( | |
| topBarHeight: Dp, | |
| bottomBarHeight: Dp, | |
| onScrollDirectionChanged: (Boolean) -> Unit, | |
| modifier: Modifier = Modifier, | |
| state: LazyListState = rememberLazyListState(), // 外部から制御したい時のために一応保持 | |
| content: LazyListScope.() -> Unit | |
| ) { | |
| // スクロール方向の検知ロジックをここに内包 | |
| LaunchedEffect(state) { | |
| var previousPosition = 0 | |
| snapshotFlow { state.firstVisibleItemIndex * 10000 + state.firstVisibleItemScrollOffset } | |
| .collect { currentPosition -> | |
| if (state.isScrollInProgress) { | |
| val delta = currentPosition - previousPosition | |
| if (abs(delta) > 15) { | |
| val visible = currentPosition < previousPosition | |
| onScrollDirectionChanged(visible) | |
| } | |
| } | |
| previousPosition = currentPosition | |
| } | |
| } | |
| LazyColumn( | |
| state = state, | |
| modifier = modifier.fillMaxSize(), | |
| // 共通の上下余白をここで強制適用 | |
| contentPadding = PaddingValues(top = topBarHeight, bottom = bottomBarHeight), | |
| content = content | |
| ) | |
| } |
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
| @Composable | |
| fun LazyListState.trackScrollDirection(onScrollDirectionChanged: (Boolean) -> Unit) { | |
| LaunchedEffect(this) { | |
| var previousPosition = 0 | |
| snapshotFlow { firstVisibleItemIndex * 10000 + firstVisibleItemScrollOffset } | |
| .collect { currentPosition -> | |
| if (isScrollInProgress) { | |
| val delta = currentPosition - previousPosition | |
| if (abs(delta) > 15) { | |
| val visible = currentPosition < previousPosition | |
| onScrollDirectionChanged(visible) | |
| } | |
| } | |
| previousPosition = currentPosition | |
| } | |
| } | |
| } |
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
| fun Modifier.trackScrollDirection( | |
| scrollState: LazyListState, | |
| onScrollDirectionChanged: (Boolean) -> Unit | |
| ): Modifier = composed { | |
| LaunchedEffect(scrollState) { | |
| var previousPosition = 0 | |
| snapshotFlow { scrollState.firstVisibleItemIndex * 10000 + scrollState.firstVisibleItemScrollOffset } | |
| .collect { currentPosition -> | |
| if (scrollState.isScrollInProgress) { | |
| val delta = currentPosition - previousPosition | |
| if (abs(delta) > 15) { | |
| val visible = currentPosition < previousPosition | |
| onScrollDirectionChanged(visible) | |
| } | |
| } | |
| previousPosition = currentPosition | |
| } | |
| } | |
| this | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compose State を Flow に変換する唯一の方法、それが snapshotFlow
https://android.benigumo.com/20260706/snapshotflow/
【Jetpack Compose】スクロール位置を正しく復元する
https://android.benigumo.com/20260624/restore-scroll-position/