简体   繁体   中英

Android Paging Library - PositionalDataSource issue

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM