How to convert a List<Object> to PagedList<Object> and vice-versa?

后端 未结 3 1332
长情又很酷
长情又很酷 2021-02-12 07:00

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

          


        
      
      
      
3条回答
  •  旧巷少年郎
    2021-02-12 08:01

    This is how I solve my problem, this might help.

    val items = List.... //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): PageKeyedDataSource() {
                override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback) {
                    callback.onResult (items, 0, items.size)
                }
        
                override fun loadAfter(params: LoadParams, callback: LoadCallback) {
        
                }
        
                override fun loadBefore(params: LoadParams, callback: LoadCallback) {
        
                }
            }
    

    Other Resources: android - how to convert list to pagedlist

提交回复
热议问题