本文介绍了如何转换List< Object>到PagedList< Object>反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PagedList<Object>用于Android的超酷分页库.为了使问题尽可能少:如果我有像这样的字符串列表

PagedList<Object> is used for Android's cool paging library. To make the question as minimal as possible :If i have a list of strings like

  List<String> stringList; // it consists of 200 strings

我想将stringList转换为键入PagedList<String>的方式

I want to convert stringList to type PagedList<String> like

  PagedList<String> pagedStringList;

如果我有一个PagedList<Object>,又如何将其转换为List<Object>呢?我经历了此内容仅供参考

And also if i have a PagedList<Object> how can convert it to a List<Object> ?I went through this for reference

如果我尝试另一种方法....

If i try the other way round ....

如何将List<Object>转换为DataSource.Factory<Integer, Object> ..以便间接地将其转换为PagedList<>?

How can I convert List<Object> into DataSource.Factory<Integer, Object> ..so that indirectly i can convert it into PagedList<> ?

推荐答案

我将从较简单的部分开始,如果要将PagedList<>转换为aaList<>,可以使用snapshot()方法,如下您可以在重新调整的文档中看到它:

I'll start with the easier part, if you want to converter a PagedList<> to a a List<> you can use the snapshot() method, as you can seen in the docs it retuns:

现在,作为另一种解决方法,您将需要使用PagedList.Builder,为此,您将需要一个数据源来创建PagedList.因此,用于管理您的静态列表的每个基本数据源都将如下所示:

Now as for the other way around you'll need to use PagedList.Builder and for that you'd need a DataSource to create the PagedList. So a avery basic dataSource that would manage your static list would look like this:

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
    }
}

如您所见,您需要重写三个方法loadInitialloadAfterloadBefore,以便告诉数据源如何从列表中加载数据块.我已经将您的列表包装在提供程序类中,只是出于组织目的.

As you can see you need to override the three methods loadInitial, loadAfter and loadBefore in order to tell the datasource how to load the cunks of data from the list. I've wrapped you list in a provider class just for organization purposes.

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构建简单的配置:

To use the data source to generate the PagedList you need to build a simple configuration with PagedList.Config.Builder:

    val myConfig = PagedList.Config.Builder()
            .setInitialLoadSizeHint(PAGE_SIZE)
            .setPageSize(PAGE_SIZE)
            .build()

,然后将其与数据源一起传递给PagedList.Builder:

And then pass that to the PagedList.Builder with the datasource:

    val pagedStrings: PagedList<String> = PagedList.Builder<Int, String>(StringDataSource(StringListProvider(originalStrings)), myConfig)
            .setInitialKey(0)
            .build()

现在您在pagedStrings中有一个PagedList.

Now you have a PagedList in pagedStrings.

java中的相同之处:

The same thing in 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();

这篇关于如何转换List&lt; Object&gt;到PagedList&lt; Object&gt;反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 22:33