[英]How to make Architecture's PagedListAdapter survives configuration changes?
Android's new PagedListAdapter is a great library for handling paging on a list of data. Android的新PagedListAdapter是一个很棒的库,用于处理数据列表上的分页。 It works well for me, only that how do you make it survives configuration changes (such as screen rotation) just like how Android Architecture's ViewModel do ?
它对我来说效果很好,只是您如何使它在配置更改(例如屏幕旋转)中幸免于难,就像Android Architecture的ViewModel一样?
The adapter depends on activity context so it wont survive configuration changes. 适配器取决于活动上下文,因此它将无法承受配置更改。 Instead, the list will be configured by the ViewModel, which survives configuration changes and updates UI accordingly.
取而代之的是,该列表将由ViewModel配置,该视图模型可以保留配置更改并相应地更新UI。 You should have something like the following.
您应该具有类似以下内容的东西。 In your activity
onCreate
: 在您的活动
onCreate
:
val adapter = CheeseAdapter()
cheeseList.adapter = adapter
// Subscribe the adapter to the ViewModel, so the items in the adapter are refreshed
// when the list changes
viewModel.allCheeses.observe(this, Observer(adapter::setList))
In your viewModel
: 在您的
viewModel
:
val allCheeses = dao.allCheesesByName().create(0,
PagedList.Config.Builder()
.setPageSize(PAGE_SIZE)
.setEnablePlaceholders(ENABLE_PLACEHOLDERS)
.build())!!
I recommend you take a look to this google sample 我建议您看看这个Google示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.