我一直在尝试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"))
惨败。欢迎任何建议:)
答案 0 :(得分:6)
最终设法使其正常工作。
前提条件-您的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)
}
哦,当在ViewHolder
中绑定onBindViewHolder
时,如果item为null,我会迅速采取行动。
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val item = getItem(position) ?: return
...
}
因为它将为空,否则适配器项数将与保存的状态项数不匹配(在此处猜测),这就是为什么在我的某些实验中,布局在第2页以上时在第1页上工作时会跳来跳去。
如果有更好的方法来解决此问题,而无需手动存储然后使用lastLayoutManagerState
,请告诉我。