1

I can get a total number of records from the server, so I am using PositionalDataSource for Network call. Everything works well but one problem: even if I don't scroll, the paging library continues fetching all the data from the server. How to solve this problem? Using: androidx.paging:paging-runtime-ktx:2.1.1

Example:

override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Doctor>) {
    fetchDataFromApi(params.requestedStartPosition, params.pageSize) { list, totalCount ->
        callback.onResult(list, 0, totalCount)
    }
}
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Doctor>) {
    fetchDataFromApi(params.startPosition, params.loadSize) { list, totalCount ->
        callback.onResult(list)
    }
}

PagedList.Config.Builder()
        .setPageSize(14)
        .setPrefetchDistance(4)
        .setInitialLoadSizeHint(14)
        .setEnablePlaceholders(true)
        .build()
GameO7er
  • 2,028
  • 1
  • 18
  • 33
sj.94
  • 123
  • 1
  • 5

1 Answers1

2

It sounds like a similar issue I spend the last two days looking into. For me it turned out, that I had wrapped my RecyclerView inside a NestedScrollView. However, the pagination library doesn't have built-in support for that according to this issue on Google tracker.

when you put recyclerview inside an infinite scrolling parent, it will layout all of its children because the parent provides infinite dimensions.

Basically the NestedScrollView continues triggering fetching more data as if the screen is as long as the complete list

Two options if this is the case for your case too:

  1. Change your layout setup to not need the NestedScrollView

  2. Here is a code example gist that helped me create a custom [SmartNestedScrollView]. Note that you have to set android:fillViewport="true" as well on this custom container and update the RecyclerView to have a height of wrap_content and remove the nestedScrollingEnabled flag from it.

sunadorer
  • 3,855
  • 34
  • 42