简体   繁体   English

如何使Architecture的PagedListAdapter不受配置更改的影响?

[英]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.

相关问题 如何最好地实现 ViewModel(在 AndroidX 中)以便数据在配置更改后仍然存在 - How Best To Implement ViewModel( in AndroidX) So Data Survives Configuration Changes ViewModel 如何在配置更改中幸存下来 - How ViewModel survives configuration change 如何将 PagedListAdapter 与多个 LiveData(s) 一起使用 - How to use PagedListAdapter with multiple LiveData(s) 清洁架构:如何在UI中反映数据层的变化 - Clean Architecture: How to reflect the data layer's changes in the UI 如何删除/移除 PagedListAdapter 项目 - How to delete/remove PagedListAdapter item 如何更新 PagedListAdapter 中的单个项目 - How to update a single item in a PagedListAdapter Android Paging Libray架构组件中PagingDataAdapter和PagedListAdapter的区别 - Difference between PagingDataAdapter and PagedListAdapter in Android Paging Libray Architecture Component 添加 ViewModel 后配置也会发生变化:Android 架构组件 - Configuration changes even after adding ViewModel : Android Architecture Component 如何从 PagedListAdapter 刷新 RecyclerView 中的数据? - How to refresh data in a RecyclerView from a PagedListAdapter? 如何在RecyclerView PagedListAdapter中的特定位置添加Admob? - How to add Admob at specific positions in RecyclerView PagedListAdapter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM