android - 修改 Android Paging Architecture 库中的 PagedList
问题描述
我目前正在考虑将分页架构库(2.1.0-beta01
撰写本文时的版本)合并到我的应用程序中。一个组件是允许用户从中删除单个项目的列表。此列表仅限网络,使用 Room 进行本地缓存没有意义。
PagedList
是不可变的,不支持修改。我已经读过,拥有一份列表副本,然后修改并作为新列表返回是要走的路。文档声明相同:
如果您有更精细的更新信号,例如网络 API 发出对列表中单个项目的更新信号,建议将数据从网络加载到内存中。然后通过包装内存快照的 DataSource 将该数据呈现给 PagedList。每次内存中的副本更改时,都会使先前的 DataSource 无效,并且可以创建一个包装快照新状态的新数据源。
我目前有基本的推荐实现来显示一个简单的列表。我的DataSource
样子是这样的:
class MyDataSource<SomeItem> : PageKeyedDataSource<Int, SomeItem>() {
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, SomeItem>) {
// Simple load from API and notification of `callback`.
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, SomeItem>) {
// Simple load from API and notification of `callback`.
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, SomeItem>) {
// Simple load from API and notification of `callback`.
}
}
文档中引用的内存缓存(没有 Room 并且没有使整个数据集失效)的具体实现是什么样的?
解决方案
如果你想修改你的列表而不一直到数据层,你需要submitList
在你的适配器中覆盖,然后在你的PagedList
对象上设置一个回调。每当PagedList
发生更改时,您都可以将这些更改复制到本地数据集。不建议这样做,但这是一个非常简单的 hack 开始工作。
这是一个例子:
class MyListAdapter : PagedListAdapter<MyDataItem, MyViewHolder>(MyDiffCallback()) {
/**
* This data set is a bit of a hack -- we are copying everything the PagedList loads into our
* own list. That way we can modify it. The docs say you should go all the way down to the
* data source, modify it there, and then bubble back up, but I don't think that will actually
* work for us when the changes are coming from the UI itself.
*/
private val dataSet = arrayListOf<MyDataItem>()
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
//Forces the next page to load when we reach the bottom of the list
getItem(position)
dataSet.getOrNull(position)?.let {
holder.populateFrom(it)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = parent.inflate(R.layout.my_view_holder)
return MyViewHolder(view)
}
class MyDiffCallback : DiffUtil.ItemCallback<MyDataItem>() {
override fun areItemsTheSame(oldItem: MyDataItem, newItem: MyDataItem) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: MyDataItem, newItem: MyDataItem) =
oldItem == newItem
}
override fun submitList(pagedList: PagedList<MyDataItem>?) {
pagedList?.addWeakCallback(listOf(), object : PagedList.Callback() {
override fun onChanged(position: Int, count: Int) {
dataSet.clear()
dataSet.addAll(pagedList)
}
override fun onInserted(position: Int, count: Int) {
dataSet.clear()
dataSet.addAll(pagedList)
}
override fun onRemoved(position: Int, count: Int) {
dataSet.clear()
dataSet.addAll(pagedList)
}
})
super.submitList(pagedList)
}
}
推荐阅读
- java - 将会话值附加到 Java 中的数组
- python - Python上的KeyError检查幅度
- android - ViewModel - 在运行时观察 LiveData 时更改方法的参数?
- assembly - PowerPC ASM:insrdi 指令
- sql-server-2008 - 在 SQL 中使用 CTE 创建表
- go - 框架 GoColly - 重定向到 HTTPS 不起作用
- git - 如何解决拉取请求中的冲突?
- java - Swing 应用程序一致性检查中的会话管理
- javascript - react-native-inline-edit 语法问题
- css - colab 不使用 css