Hi, I am trying to develop a horizontal swipe navi...
# compose
j
Hi, I am trying to develop a horizontal swipe navigation by using accompanist library's
HorizontalPager
. By testing the sample code from the guide works great:
Copy code
// Display 10 items
HorizontalPager(count = 10) { page ->
    // Our page content
    Text(
        text = "Page: $page",
        modifier = Modifier.fillMaxWidth()
    )
}
But I need to display different `Composable`s in different pages instead of same element. The only way I figured how to do this is the following:
Copy code
HorizontalPager(count = 3) {
    if(currentPage == 0){
        FirstView() // Composable
     } else if (currentPage == 1) {
        SecondView() // Composable
     } else if (currentPage == 2){
        ThirdView() // Composable
     }
The problem with this implementation is that when I swipe to navigate, it appears like the view coming from the side is the same as the current view. When the
currentpage
updates to the next view it suddenly updates the view to the upcoming view. So there is no smooth animation like in the sample code. Suggestions how to implement this better?
s
You are not using the
it
(
page
parameter) in the second snippet
j
For some reason I thought that I do not need it so I purposefully deleted it.. Added the
page
parameter and it works wonders. Thanks a lot!
a
I recommend using this :
Copy code
HorizontalPager(count = 3) { page ->
when (page) {
0 -> FirstView()
1 -> Second View()
2 -> ThirdView()
}