gpt4 book ai didi

android - 恢复 Activity 时恢复 PagedListAdapter 位置

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:43:27 28 4
gpt4 key购买 nike

我一直在试验 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恢复时 - 适配器将位于 beginning of page #4 的位置.理想情况下,适配器应恢复到与上次看到的位置完全相同的位置。

保存LayoutManager状态的各种尝试

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

然后恢复

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

惨遭失败。欢迎任何建议:)

最佳答案

终于成功了。

先决条件 - 您的 PagedListAdapter 必须支持 null 占位符! 设置启用占位符(真)。阅读更多 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 中绑定(bind) ViewHolder 时,如果 item 为空,我会快速退出。

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

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

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

关于android - 恢复 Activity 时恢复 PagedListAdapter 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51668858/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com