恢复时恢复 PagedListAdapter 位置 activity

Restore PagedListAdapter position when resuming activity

我一直在试验 PagedListAdapter,但不知道如何正确恢复适配器位置。

上次尝试是从当前列表中保存 lastKey

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

    val lastKey = adapter.currentList?.lastKey as Int

    outState.putInt("lastKey", lastKey)
}

但是当恢复我的适配器并将 lastKey 传递给 PagedListBuilder 时,我最后看到的和显示的有很大不同。

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

如果我在 middle of page #4 when resuming - adapter will be at position at the beginning of page #4。理想情况下,适配器应恢复到与上次看到的位置完全相同的位置。

保存 LayoutManager 状态的各种尝试

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

然后恢复

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

惨败。欢迎提出任何建议:)

终于成功了。

先决条件 - 您的 PagedListAdapter 必须支持空占位符 setEnablePlaceholders(true)。阅读更多 here

    val dataSourceFactory = dao.reportsDataSourceFactory()
    val builder = RxPagedListBuilder(
        dataSourceFactory,
        PagedList.Config.Builder()
            .setEnablePlaceholders(true) //in my original implementation it was false
            .setInitialLoadSizeHint(60)
            .setPageSize(20)
            .setPrefetchDistance(60)
            .build()
    )

照常保存状态:

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

    val lastKey = adapter.currentList?.lastKey as Int

    outState.putInt("lastKey", lastKey)
    outState.putParcelable("layout_manager_state", recycler_view.layoutManager.onSaveInstanceState())
}

但是在恢复时 - 首先将状态保存为变量,并且只有在将列表提交到 PagedListAdapter

后才恢复保存的状态
private fun showReports(pagedList: PagedList<Report>?) {
    adapter.submitList(pagedList)

    lastLayoutManagerState?.let {
        report_list.layoutManager.onRestoreInstanceState(lastLayoutManagerState)
        lastLayoutManagerState = null
    }
}

其中 lastLayoutManagerState 是:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    viewModel = withViewModel(viewModelFactory) {
        observe(reports, ::showReports)
    }

    report_list.adapter = adapter

    lastLayoutManagerState = savedInstanceState?.getParcelable("layout_manager_state")

    val lastKey = savedInstanceState?.getInt("lastKey")
    viewModel.getReports(lastKey)
}

哦,当在 onBindViewHolder 中绑定 ViewHolder 时,如果项目为空,我会快速退出。

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val item = getItem(position) ?: return
    ... 
}

因为它将为 null,否则适配器项数将与保存的状态项数不匹配(此处猜测),这就是为什么在我的一些实验中布局在页面 2+ 上跳来跳去,而它在页面上工作1.

如果有更好的方法来解决这个问题而无需手动存储然后使用 lastLayoutManagerState - 让我知道。