android-jetpack-composejetpack-compose-accompanist

How to add multiple items to Accompanist HorizontalPager?


When I create

HorizontalPager(count = 3) {
   page  -> ComposableName()
}

It just shows same composable multiple times, i can't add few pages and if I try to

HorizontalPager(count = 3) {
   Image()
   Image()
   Image()
}

elements are just stacked on top of each other and also 3 times.

Is there any way to make smth like

LazyColumn()
{
item {}
item {}
item {}
}

If not what's the simplest solution?


Solution

  • In the content block of HorizontalPager you get index of the page that should be displayed. So you can do something like this:

    HorizontalPager(count = 3) { index ->
        if (index == 0) {
            Image0()
        } else if (index == 1) {
            Image1()
        }
        ...
    }