android-viewpagerandroid-jetpack-composejetpack-compose-accompanist

How to scroll view pager (accompanist library) on button click in jetpack compose Android


i want to scroll the view pager horizontally on button click in jetpack compose.Anyone have any idea about this ? Here i am using Accompanist library.


Solution

  • You need to use pager state like this:

    val state = rememberPagerState()
    val scope = rememberCoroutineScope()
    Button(onClick = {
        scope.launch {
            state.scrollToPage(state.currentPage + 1)
            // or 
            state.scrollBy(100f)
        }
    }) {
    
    }
    VerticalPager(
        pagesCount,
        state = state
    ) { page ->
    
    }