I am just trying to make a simple Pager which shows a text with the page number while scrolling. It should scroll forward and back wards infinitely. But for some reason it keeps increasing the page number while not scrolling. I can not figure out why. Here is my code please help.
PagingSource
class MyPagingSource : PagingSource<Int, String>() {
override fun getRefreshKey(state: PagingState<Int, String>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, String> {
val nextPage = params.key ?: 0
val data = listOf("Page $nextPage")
val prevKey = if (nextPage > 0) nextPage.minus(1) else null
val nextKey = if (data.isNotEmpty()) nextPage.plus(1) else null
return LoadResult.Page(
data = data,
prevKey = nextKey,
nextKey = prevKey
)
}
}
ViewModel
class MyViewModel: ViewModel() {
val pager = Pager(
config = PagingConfig(pageSize = 100),
pagingSourceFactory = { MyPagingSource() },
)
.flow
}
Screen
@Composable
@Preview
fun App() {
val viewmodel = koinViewModel<MyViewModel>()
val lazyPagingItems = viewmodel.pager.collectAsLazyPagingItems()
val state = rememberPagerState(initialPage = 50) { lazyPagingItems.itemCount }
HorizontalPager(
modifier = Modifier.fillMaxSize(),
state = state,
) { page ->
val item = lazyPagingItems[page]
Text(text = "Item: $item")
}
}
Source: View source