PagedList<Object>
用于Android的酷炫分页库。要使问题尽可能小:
如果我有一个像
List<String> stringList; // it consists of 200 strings
我想将stringList
转换为{/ 1}}类似
PagedList<String>
如果我有 PagedList<String> pagedStringList;
,如何将其转换为PagedList<Object>
?
我经历了this for reference
如果我尝试相反的方式......
如何将List<Object>
转换为List<Object>
..那么间接我可以将其转换为DataSource.Factory<Integer, Object>
?
从
PagedList<>
我可以转换为DataSource.Factory<Integer, Object>
,但如何将PagedList<>
转换为list
?
答案 0 :(得分:7)
如果你想转换一个PagedList&lt;&gt;,我将从更容易的部分开始。列表&lt;&gt;您可以使用snapshot()
方法,正如您在其返回的文档中看到的那样:
返回当前状态的PagedList的不可变快照.PagedList快照只是作为List的PagedList当前状态的不可变浅表副本。
现在反过来说,你需要使用PagedList.Builder,为此你需要一个DataSource来创建PagedList。因此,管理静态列表的avery basic dataSource将如下所示:
<强> StringDataSource.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
,以告知数据源如何从列表中加载数据块。我已经将您的列表包装在提供程序类中,仅用于组织目的。
<强> StringListProvider.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()
现在你在pagedStrings
中有一个PagedList。
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);
}
}
<强> StringDataSource.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();
答案 1 :(得分:0)
您可以使用addAll
方法,因为PagedList
是AbstractList
。
答案 2 :(得分:0)
我已通过kotlin中的这段代码将List<Object>
转换为PagedList<Object>
我认为这段代码可以帮助您
构建config
private val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPageSize(LIST_SIZE)
.build()
2。创建这些类进行转换并返回我的PagedList<Object>
class ListDataSource<T>(private val items: List<T>) : PositionalDataSource<T>() {
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<T>) {
callback.onResult(items, 0, items.size)
}
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<T>) {
val start = params.startPosition
val end = params.startPosition + params.loadSize
callback.onResult(items.subList(start, end))
}
}
// UiThreadExecutor implementation example
class UiThreadExecutor : Executor {
private val handler = Handler(Looper.getMainLooper())
override fun execute(command: Runnable) {
handler.post(command)
}
}
通过val myList: List<Object>
向config
传递并获得PagedList<Object>
val pagedList = PagedList.Builder(ListDataSource(myList), config)
.setNotifyExecutor(UiThreadExecutor())
.setFetchExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
.build()
答案 3 :(得分:0)
这就是我解决问题的方式,这可能会有所帮助。
val items = List<Item>.... //this can be equal to your mutable list
val config = PagedList.Config.Builder()
.setPageSize(items.size)
.setEnablePlaceholders(false)
.setInitialLoadSizeHint(items.size)
.build()
val pagedList = PagedList.Builder(ListDataSource(items),config)
.setNotifyExecutor (UiThreadExecutor ())
.setFetchExecutor (AsyncTask.THREAD_POOL_EXECUTOR)
.build ()
class UiThreadExecutor: Executor {
private val handler = Handler (Looper.getMainLooper ())
override fun execute (command: Runnable) {
handler.post (command)
}
}
class ListDataSource (private val items: List<Item>): PageKeyedDataSource<Int, Item>() {
override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, Item>) {
callback.onResult (items, 0, items.size)
}
override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, Item>) {
}
override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Int, Item>) {
}
}