

pagerState.animateScrollToPager(index) 함수 사용LaunchedEffect(key1 = pagerState.currentPage) {
delay(3_000)
pagerState.animateScrollToPage(pagerState.currentPage.inc())
}anomateScrollToPage(index) 함수의 구현부를 살펴보자suspend fun animateScrollToPage(
@IntRange(from = 0) page: Int,
@FloatRange(from = -1.0, to = 1.0) pageOffset: Float = 0f,
) {
...
try {
...
} finally {
onScrollFinished()
}
onScrollFinished() 함수에 아래와 같이 주석이 작성되어 있음
We need to manually call this, as the
animateScrollToItemcall above will happen in 1 frame, which is usually too fast for the LaunchedEffect in Pager to detect the change. This is especially true when running unit tests.
animateScrollToItem(index)는 1프레임에서 발생되므로 수동으로 호출하는 것을 권장하고, Pager의 변경 사항을 LaunchedEffect가 감지하기 너무 빠르다고 함정리하자면, LaunchedEffect에 Pager와 관련된 것을 Key로 사용하면 감지가 너무 빨라서 animateScrollToItem(index)가 끝나기 전에 다음 LaunchedEffect가 실행됨
val isDraggedState: State<Boolean> =
pagerState.interactionSource.collectIsDraggedAsState()
LaunchedEffect(key1 = isDraggedState) {
snapshotFlow { isDraggedState.value }
.collectLatest { isDragged ->
if (isDragged) return@collectLatest
while (true) {
delay(3_000)
pagerState.animateScrollToPage(pagerState.currentPage.inc())
}
}
}