1

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.

mallaudin
  • 4,744
  • 3
  • 36
  • 68

2 Answers2

1

I have created a class MovieListDataSource that extends PositionalDataSource. The fact is you have to use PagedListAdapter and this will call loadAround(int). If everything works well then there will be no loading issue. Click here and you will get my implementation. Hope this will help you.

Anjan Debnath
  • 120
  • 1
  • 8
0

I was using the wrong range for (i in params.startPosition..params.loadSize). Worked after setting the correct range params.startPosition + params.loadSize.

mallaudin
  • 4,744
  • 3
  • 36
  • 68