gpt4 book ai didi

android - 修改 Android Paging Architecture 库中的 PagedList

转载 作者:IT老高 更新时间:2023-10-28 13:36:15 24 4
gpt4 key购买 nike

我目前正在考虑将分页架构库(在撰写本文时版本为 2.1.0-beta01)整合到我的应用程序中。一个组件是允许用户从中删除单个项目的列表。此列表仅限网络,使用 Room 进行本地缓存没有意义。

PagedList 是不可变的,不支持修改。我已经读过,拥有一份列表副本,然后修改并作为新列表返回是要走的路。文档声明相同:

If you have more granular update signals, such as a network API signaling an update to a single item in the list, it's recommended to load data from the network into memory. Then present that data to the PagedList via a DataSource that wraps an in-memory snapshot. Each time the in-memory copy changes, invalidate the previous DataSource, and a new one wrapping the new state of the snapshot can be created.

我目前有基本的推荐实现来显示一个简单的列表。我的 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)
}
}

关于android - 修改 Android Paging Architecture 库中的 PagedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53243704/

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