Tuesday, 7 August 2018

Restore PagedListAdapter position when resuming activity

I've been experimenting with PagedListAdapter and can't figure out how to restore adapters position correctly.

Last attempt was to save lastKey from current list.

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)

    val lastKey = adapter.currentList?.lastKey as Int

    outState.putInt("lastKey", lastKey)
}

but when restoring my adapter and passing lastKey to PagedListBuilder what I last saw and what is being displayed differs by quite a bit.

    val dataSourceFactory = dao.reportsDataSourceFactory()
    val builder = RxPagedListBuilder(
        dataSourceFactory,
        PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setInitialLoadSizeHint(60)
            .setPageSize(20)
            .setPrefetchDistance(60)
            .build()
    )
        .setInitialLoadKey(initialLoadKey)
        .setBoundaryCallback(boundaryCallback)

If I'm in the middle of page #4 when resuming - adapter will be at position at the beginning of page #4. Ideally adapter should be restored in exactly the same position as last seen.

Various attempts to save LayoutManager state

outState.putParcelable("layout_manager_state", recycler_view.layoutManager.onSaveInstanceState()) 

and then restore it

recycler_view.layoutManager.onRestoreInstanceState(it.getParcelable("layout_manager_state"))

failed miserably. Any suggestions are welcome :)



from Restore PagedListAdapter position when resuming activity

No comments:

Post a Comment