I'm trying to track infinite scrolling. I've implemented the tracking in pagingsource. When I open the page, paging loads 2 pages. And without even scrolling, the track request is sending. How can i stop paging from loading 2 pages ?
ExamplePagingSource: PagingSource<String, UIItem>() {
private var scrollDepth = 0
override suspend fun load(params: LoadParams<String>): LoadResult<String, UIItem> {
val pageKey = params.key ?: ""
return try {
val exampleData = getExampleData()
scrollDepth++
trackScroll()
LoadResult.Page(
data = items,
prevKey = if (isFirstPage) null else pageKey,
nextKey = exampleData.pageKey.ifEmpty { null }
)
} catch (e: Exception) {
Timber.e(e, "Error")
LoadResult.Error(e)
}
}
UPDATE I solved it with prefetchDistance
I solved it with prefetchDistance. If you set prefetch distance to 2. Paging library call api at second to last item. You must set in PagingConfig
Pager(
config = PagingConfig(
pageSize = PAGE_SIZE,
prefetchDistance = PREFETCH_DISTANCE
),
pagingSourceFactory = ::pagingSource
)