Paging分頁面是google推出的一個結合RecyclerView進行分頁加載數據的一個全新架構庫,主要是爲了解決一次性加載大量數據而形成的資源浪費問題。經過分頁的方式,每次加載一頁數據,既能夠加快界面的渲染,又能夠減小對象等資源的建立消耗。具體能夠看官網android
分頁庫主要由如下三個部分組成git
DataSource: 數據源,定義獲取數據的方式,有三種方式,分別是github
1. PageKeyedDataSource
2. ItemKeyedDataSource
3. PositionalDataSource. 基於位置信息進行數據的加載,和Room數據庫或者本地數據源一塊兒搭配。
複製代碼
PagedListAdapter: 分頁庫適配器,繼承於RecyclerView的適配器,內部須要實現一個DiffUtil.ItemCallback差分器分析數據是否發生了改變。數據庫
PagedList: 定義分頁庫的配置,分別有默認加載數據大小,分頁數據大小等。而且經過PagedListAdapter將數據的變化進行更新。bash
因爲這次使用的是本地數據,因此須要的列表的位置信息,在這裏,咱們須要實現基於PositionalDataSource的數據源架構
class LocalDataSourceFactory:DataSource.Factory<Int, ArticleEntity>() {
override fun create(): DataSource<Int, ArticleEntity> {
return localDataSource
}
companion object {
val localDataSource = object : PositionalDataSource<ArticleEntity>() {
private fun computeCount(): Int {
return 10000
}
private fun loadRangeInternal(startPosition: Int, loadCount: Int): List<ArticleEntity> {
val articleList = mutableListOf<ArticleEntity>()
val authorPrefix = "做者"
val titlePrefix = "我是一個標題"
val typePrefix = "類別"
val timeStampBase = 1531548138000L
for (i in 0 until loadCount) {
var articleEntity = ArticleEntity()
articleEntity.id = (startPosition + i).toString()
articleEntity.author = "$authorPrefix ${articleEntity.id}"
articleEntity.title = "$titlePrefix ${articleEntity.id}"
articleEntity.type = "$typePrefix ${articleEntity.id}"
articleEntity.timeStamp = timeStampBase + i * 1000L
articleList.add(articleEntity)
}
return articleList
}
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<ArticleEntity>) {
Log.e("LoadRange", "range" + params.startPosition)
callback.onResult(loadRangeInternal(params.startPosition, params.loadSize))
}
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<ArticleEntity>) {
val totalCount = computeCount()
val position = PositionalDataSource.computeInitialLoadPosition(params, totalCount)
val loadSize = PositionalDataSource.computeInitialLoadSize(params, position, totalCount)
callback.onResult(loadRangeInternal(position, loadSize), position, totalCount)
}
}
}
}
複製代碼
須要實現PositionalDataSource的兩個方法,分別是loadInitial和loadRange,loadInitial負責拉取配置的加載條數,即下文的PagedList配置, loadRange負責加載每次分頁所需的數據。因此實現數據源很簡單,只需定義好首次加載數據和分頁加載數據的邏輯既可。ide
因爲PagedListAdapter繼承自RecyclerView的適配器,因此實現起來並不難,只是須要提供一個差分的實現用來進行數據的分析,代碼以下:post
class ArticlePageAdapter : PagedListAdapter<ArticleEntity, ArticleViewHolder>(diffCallback) {
override fun onBindViewHolder(holder: ArticleViewHolder, position: Int) {
holder.bindTo(getItem(position))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ArticleViewHolder =
ArticleViewHolder(parent)
companion object {
private val diffCallback = object : DiffUtil.ItemCallback<ArticleEntity>() {
override fun areItemsTheSame(oldItem: ArticleEntity, newItem: ArticleEntity): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: ArticleEntity, newItem: ArticleEntity): Boolean =
oldItem == newItem
}
}
}
複製代碼
PagedList主要是設置分頁的大小,初始化加載的數據大小等配置。學習
val pagedListConfig =PagedList.Config.Builder().setEnablePlaceholders(true).setPageSize(10).setInitialLoadSizeHint(20).build()
var postList = LivePagedListBuilder(LocalDataSourceFactory(), pagedListConfig).build()
複製代碼
經過以上代碼生成是一個帶LiveData的PagedListui
生成DataSource負責數據來源, 接着實現PagedListAdapter負責UI的渲染,最後進行PagedList分頁的一些配置。生成一個帶LiveData的PagedList,一旦數據進行變化,便會通知pageAdapter調用submitList進行UI的更新
class LocalDataPagingActivity:AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.act_local_data_paging)
val pageAdapter = ArticlePageAdapter()
recycle_article.adapter = pageAdapter
recycle_article.layoutManager = LinearLayoutManager(this)
val pagedListConfig = PagedList.Config.Builder().setEnablePlaceholders(true).setPageSize(10).setInitialLoadSizeHint(20).build()
var postList = LivePagedListBuilder(LocalDataSourceFactory(), pagedListConfig).build()
postList.observe(this, Observer {
pageAdapter.submitList(it)
})
}
}
複製代碼
demo已經上傳,點擊傳送門,若有疑惑或者錯誤,歡迎指出。
另一個傳送門Android Paging分頁庫的學習(二)—— 結合Room數據庫進行分頁加載