最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin6PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far