I am working on sample where I want to display items using HorizontalPager. I am able to display pages horizontally scrollable without issues. Also able to place indicators using HorizontalPagerIndicator.
Issue: When I click on pager Indicator I am able to navigate forward without any issues. I have 4 pages , when clicked on zerothIndicator , page is scrolled to zeroth indicator, when clicked on 1st indicator page is scrolled to 1st page, when again clicked on zeroth Indicator instead of scrolling to Zeroth page , its scrolled to 2nd page. I am not able to track the position of clicked indicator. Is there a way can we track the position of clicked indicator. Please find my code below for reference:
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HorizontalPagerWithIndicators(deals: List<Deal>) {
val pagerState = rememberPagerState()
val coroutineScope = rememberCoroutineScope()
Column{
HorizontalPager(pageCount = deals.size, state = pagerState,
contentPadding = PaddingValues(horizontal = 20.dp), pageSpacing = 10.dp) { page ->
Column() {
DisplayDeal(deal = deals[page])
Box(
modifier = Modifier
.align(Alignment.CenterHorizontally)
.padding(top = 10.dp, bottom = 10.dp)
) {
HorizontalPagerIndicator(
pageCount = deals.size,
pagerState = pagerState,
modifier = Modifier.align(Alignment.Center).clickable {
val currentPage = pagerState.currentPage
val totalPages = deals.size
val nextPage = if (currentPage < totalPages - 1) currentPage + 1 else 0
coroutineScope.launch { pagerState.animateScrollToPage(nextPage) }
}
)
}
}
}
}
}
Below are the dependencies used:
implementation 'androidx.compose.foundation:foundation:1.4.3'
implementation "com.google.accompanist:accompanist-pager-indicators:0.30.1"
Please help me in resolving this issue.
Thanks in Advance.