I am trying to experiment with Positional DataSource. I have created the following dummy source.
class ContactsDataSource : PositionalDataSource<Contact>() {
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Contact>) {
val c = ArrayList<Contact>()
for (i in params.startPosition..params.loadSize) {
c.add(Contact("$i contact"))
}
callback.onResult(c)
Logger.d("loadRange: StartPos: ${params.startPosition} LoadSize: ${params.loadSize}")
}
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Contact>) {
Logger.d("Load Initial: PageSize: ${params.pageSize} LoadSize: ${params.requestedLoadSize}")
val c = ArrayList<Contact>()
for (i in 0 until params.requestedLoadSize) {
c.add(Contact("$i contact"))
}
callback.onResult(c, 0, 1000)
}
}
class ContactsDsFactory : DataSource.Factory<Int, Contact>() {
override fun create(): DataSource<Int, Contact> {
return ContactsDataSource()
}
}
For the first time, it calls loadInitial
and items are loaded. When I scroll up, it loads more items by calling loadRange
. After load range is called for the first time, it is never called again. Although, the items loaded are less than the total count I have passed in loadInitial
callback. Here is how I have setup the source.
val config = PagedList.Config.Builder().setEnablePlaceholders(false)
.setMaxSize(120)
.setInitialLoadSizeHint(60)
.setPageSize(20).build()
val list = LivePagedListBuilder<Int, Contact>(ContactsDsFactory(), config)
.build()
val recycler = findViewById<RecyclerView>(R.id.recycler)
recycler.layoutManager = LinearLayoutManager(this)
val adapter = ContactsRecycler()
recycler.adapter = adapter
list.observe(this, Observer {
adapter.submitList(it)
})
These are the log
D/PagedLab: Load Initial: PageSize: 20 LoadSize: 60
D/PagedLab: loadRange: StartPos: 60 LoadSize: 20
Any help will be appreciated.