How to stop auto scroll VerticalPager jetpack compose if user swiping?

209 Views Asked by At

I have VerticalPager and demo auto scroll for him in LaunchedEffect. I want 3 conditions to stop auto scroll:

  1. Stop after scroll of 3 slide - done this in code it`s ok
  2. Stop on button press - done this in code, but does not work well, after clicking it scrolls at 1 slide. I need a hard stop. (help me)
  3. Stop auto scroll if user do vertical swipe. (help me)

@SuppressLint("SuspiciousIndentation")
@ExperimentalPagerApi
@Composable
fun ViewPagerSlider() {
    var autoScroll = remember { mutableStateOf(0) }        

    val pagerState = rememberPagerState(
        pageCount = CatalogList.size,
        initialPage = 0,
        infiniteLoop = true
    )

    LaunchedEffect(Unit) {
        while(++autoScroll.value <= 3) {
            yield()
            delay(2000)
            pagerState.animateScrollToPage(
                page = (pagerState.currentPage + 1), 
                animationSpec = tween(600)
            )
        }
    }


    Box() {
        VerticalPager(
            state = pagerState,
            dragEnabled = userScroll.value,
            modifier = Modifier
                .fillMaxSize()
        ) { indexPage ->
            Column(
                verticalArrangement = Arrangement.Center,
                modifier = Modifier
                    .fillMaxSize()
            ) {
              //////// CONTENT /////////
              }
          }
       
        IconButton(
          modifier = Modifier
                      .onClick = {
                          autoScroll.value = 5
                      }
          ) {
            Icon(
                imageVector = Icons.Filled.Info,
                contentDescription = "Info",
                tint = Color.Red,
                )
         }
    }
}           

I tried using pointerInput with him attributes - does not work, detectDragGestures - does not work, draggable work only inside the VerticalPager applicable to Column, but it do manual swiping VerticalPager is not possible.

I think that need detect the process of manual scrolling, But known attributes don't allow it. number identification the current page no difference between it and auto scroll.

Help me please

0

There are 0 best solutions below