admin管理员组文章数量:1026989
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 Answer
Reset to default 1The 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.
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 causesisPaused
alwaysfalse
– Abdo21 Commented Nov 16, 2024 at 11:46
1 Answer
Reset to default 1The 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.
本文标签: androidauto scroll horizontal pager inside LaunchedEffect state not handledStack Overflow
版权声明:本文标题:android - auto scroll horizontal pager inside LaunchedEffect state not handled - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745662902a2162000.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
isPaused = false
inside the pager content block. This causesisPaused
alwaysfalse
– Abdo21 Commented Nov 16, 2024 at 11:46