我将从更简单的部分开始,如果您想将 PagedList<> 转换为 aa List<> 您可以使用该snapshot()
方法,正如您在它返回的文档中所见:
返回当前状态的 PagedList 的不可变快照。PagedList 快照只是作为 List 的 PagedList 当前状态的不可变浅表副本。
现在至于其他方式,您需要使用 PagedList.Builder,为此您需要一个 DataSource 来创建 PagedList。因此,管理静态列表的非常基本的数据源如下所示:
字符串数据源.kt
class StringDataSource(val provider: StringListProvider) : PageKeyedDataSource<Int, String>() {
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, String>) {
val list = provider.getStringList(0, params.requestedLoadSize)
callback.onResult(list, 1, 2)
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, String>) {
val list = provider.getStringList(params.key, params.requestedLoadSize)
callback.onResult(list, params.key + 1)
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, String>) {
val list = provider.getStringList(params.key, params.requestedLoadSize)
val nextIndex = if (params.key > 1) params.key - 1 else null
callback.onResult(list, nextIndex)
}
companion object {
const val PAGE_SIZE = 20
}
}
如您所见,您需要重写这三个方法,loadInitial
以便告诉数据源如何从列表中加载数据块。出于组织目的,我已将您的列表包装在提供程序类中。loadAfter
loadBefore
字符串列表提供者.kt
class StringListProvider(val list: List<String>) {
fun getStringList(page: Int, pageSize: Int): List<String> {
val initialIndex = page * pageSize
val finalIndex = initialIndex + pageSize
//TODO manage index out of range
return list.subList(initialIndex, finalIndex)
}
}
要使用数据源生成 PagedList,您需要构建一个简单的配置PagedList.Config.Builder
:
val myConfig = PagedList.Config.Builder()
.setInitialLoadSizeHint(PAGE_SIZE)
.setPageSize(PAGE_SIZE)
.build()
然后将其传递给PagedList.Builder
数据源:
val pagedStrings: PagedList<String> = PagedList.Builder<Int, String>(StringDataSource(StringListProvider(originalStrings)), myConfig)
.setInitialKey(0)
.build()
现在你有一个 PagedList in pagedStrings
。
在java中同样的事情:
StringListProvider.java
public class StringListProvider {
private List<String> list;
public StringListProvider(List<String> list) {
this.list = list;
}
public List<String> getStringList(int page, int pageSize) {
int initialIndex = page * pageSize;
int finalIndex = initialIndex + pageSize;
//TODO manage out of range index
return list.subList(initialIndex, finalIndex);
}
}
字符串数据源.java
public class StringDataSource extends PageKeyedDataSource<Integer, String> {
public static final int PAGE_SIZE = 20;
private StringListProvider provider;
public StringDataSource(StringListProvider provider) {
this.provider = provider;
}
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, String> callback) {
List<String> result = provider.getStringList(0, params.requestedLoadSize);
callback.onResult(result, 1, 2);
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
List<String> result = provider.getStringList(params.key, params.requestedLoadSize);
Integer nextIndex = null;
if (params.key > 1) {
nextIndex = params.key - 1;
}
callback.onResult(result, nextIndex);
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
List<String> result = provider.getStringList(params.key, params.requestedLoadSize);
callback.onResult(result, params.key + 1);
}
}
配置生成
PagedList.Config myConfig = new PagedList.Config.Builder()
.setInitialLoadSizeHint(PAGE_SIZE)
.setPageSize(PAGE_SIZE)
.build();
列表转换
List<String> myList = new ArrayList<>();
StringListProvider provider = new StringListProvider(myList);
StringDataSource dataSource = new StringDataSource(provider);
PagedList<String> pagedStrings = new PagedList.Builder<Integer, String>(dataSource, myConfig)
.setInitialKey(0)
.build();