最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - auto scroll horizontal pager inside LaunchedEffect state not handled - Stack Overflow

programmeradmin7浏览0评论

I am working on a auto scroller for my horizontal pager. Is should auto scroll every 3 seconds. Should be able to skip when clicked. And pause when longClicked. When in the paused state. It will resume when the user swipes or clickes to skip. However, my isPaused state always seems to be false in the LaunchedEffect. I think there is something wrong with the way I am handling my state.

I set the isPaused to intialize as false. As we are not paused when first entering the screen

When longClicked I set the isPaused to true

And to resume in the click or swipe I set back to false.

This looks strange as I set to true, but the LaunchedEffect gets triggered twice, and is false

onLongClick true
LaunchedEffect false
LaunchedEffect false
    val coroutineScope = rememberCoroutineScope()
    var isPaused by remember { mutableStateOf(false) }
    LaunchedEffect(isPaused) {
        println("LaunchedEffect $isPaused")
        
        while (!isPaused) {
            delay(3_000)
            var nextPage = pagerState.currentPage + 1
            if (nextPage >= items.count()) {
                nextPage = 0
            }
            pagerState.animateScrollToPage(nextPage)
        }
    }

My horizontal pager looks like this

HorizontalPager(
            state = pagerState,
            pageSpacing = 16.dp,
            modifier = modifier
                .fillMaxSize()
                binedClickable(
                    onLongClick = {
                        isPaused = true
                        println("onLongClick $isPaused")
                    },
                    onClick = {
                        if(pagerState.currentPage < items.count()) {
                            isPaused = false
                            println("onClick $isPaused")
                            coroutineScope.launch {
                                pagerState.animateScrollToPage(pagerState.currentPage + 1)
                            }
                        }
                    }
                )
        ) { pageIndex ->
            when(pageIndex) {
                in items.indices -> {
                    isPaused = false
                    content(items[pageIndex])
                }

                else -> {
                    lastItemContent?.invoke()
                }
            }
        }

I am working on a auto scroller for my horizontal pager. Is should auto scroll every 3 seconds. Should be able to skip when clicked. And pause when longClicked. When in the paused state. It will resume when the user swipes or clickes to skip. However, my isPaused state always seems to be false in the LaunchedEffect. I think there is something wrong with the way I am handling my state.

I set the isPaused to intialize as false. As we are not paused when first entering the screen

When longClicked I set the isPaused to true

And to resume in the click or swipe I set back to false.

This looks strange as I set to true, but the LaunchedEffect gets triggered twice, and is false

onLongClick true
LaunchedEffect false
LaunchedEffect false
    val coroutineScope = rememberCoroutineScope()
    var isPaused by remember { mutableStateOf(false) }
    LaunchedEffect(isPaused) {
        println("LaunchedEffect $isPaused")
        
        while (!isPaused) {
            delay(3_000)
            var nextPage = pagerState.currentPage + 1
            if (nextPage >= items.count()) {
                nextPage = 0
            }
            pagerState.animateScrollToPage(nextPage)
        }
    }

My horizontal pager looks like this

HorizontalPager(
            state = pagerState,
            pageSpacing = 16.dp,
            modifier = modifier
                .fillMaxSize()
                binedClickable(
                    onLongClick = {
                        isPaused = true
                        println("onLongClick $isPaused")
                    },
                    onClick = {
                        if(pagerState.currentPage < items.count()) {
                            isPaused = false
                            println("onClick $isPaused")
                            coroutineScope.launch {
                                pagerState.animateScrollToPage(pagerState.currentPage + 1)
                            }
                        }
                    }
                )
        ) { pageIndex ->
            when(pageIndex) {
                in items.indices -> {
                    isPaused = false
                    content(items[pageIndex])
                }

                else -> {
                    lastItemContent?.invoke()
                }
            }
        }
Share Improve this question edited Nov 16, 2024 at 8:33 ant2009 asked Nov 16, 2024 at 8:27 ant2009ant2009 22.8k167 gold badges436 silver badges645 bronze badges 1
  • 1 You are setting isPaused = false inside the pager content block. This causes isPaused always false – Abdo21 Commented Nov 16, 2024 at 11:46
Add a comment  | 

1 Answer 1

Reset to default 1

The problem lies within this code bit:

in items.indices -> {
    isPaused = false  // HERE
    content(items[pageIndex])
}

Whenever a recomposition of the content of HorizontalPager occurs, isPaused will be reset to false.
To set isPaused to false only when the user manually is swiping, you can use another LaunchedEffect like this:

LaunchedEffect(pagerState.currentPage) {
    isPaused = false
}

The content might get recomposed whenever the isPaused changes because the whole HorizontalPager is within the same recomposition scope.

发布评论

评论列表(0)

  1. 暂无评论