26

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

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

  PagedList<String> pagedStringList;

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

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

From DataSource.Factory<Integer, Object> I can convert to PagedList<> but how can I convert list into PagedList<> ?


Reset index seems like the right way to go, seems like you don't want it to be an index (the result you're getting is the way indexes work).

s.reset_index(level=2).groupby(level=[0])['third'].unique()

or if you want counts

s.reset_index(level=2).groupby(level=[0])['third'].value_counts()
4

3 回答 3

10

我将从更简单的部分开始,如果您想将 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以便告诉数据源如何从列表中加载数据块。出于组织目的,我已将您的列表包装在提供程序类中。loadAfterloadBefore

字符串列表提供者.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();
于 2018-05-13T11:31:50.173 回答
2

我已经在 kotlinList<Object>中通过这段代码转换为我认为这段代码可以帮助你PagedList<Object>

  1. 建造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)
    }
}
  1. 路过val myList: List<Object>得到config_PagedList<Object>

    val pagedList = PagedList.Builder(ListDataSource(myList), config)
                                .setNotifyExecutor(UiThreadExecutor())
                                .setFetchExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
                                .build()
    
于 2020-06-13T06:18:07.487 回答
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>) {
    
            }
        }

其他资源:android - 如何将列表转换为分页列表

于 2020-08-04T03:44:31.697 回答