Paging

Paging

介绍

1
The Paging Library helps you load and display small chunks of data at a time.Loading partial data on demand reduces usage of network bandwidth and system

处理大量数据的处理方法:

  1. 借助刷新控件实现用户手动请求数据。
  2. 数据到达边界自动请求加载。
名称 作用
PagedList 一个可以以分页形式异步加载数据的容器,可以跟RecyclerView很好的结合
DataSource或DataSource.Factory 数据源,DataSource将数据转换为PagedList,DataSource.Factory则用来创建DataSource
LivePagedListBuilder 用来生成LiveData<PagedList>,需要DataSource.Factory参数
BoundaryCallback 数据到达边界的回调
PagedListAdapter 一种RecyclerView的适配器

优点

  • RxJava 2以及Android Jetpack的支持,如LiveDataRoom
  • 自定义分页策略。
  • 异步处理数据。
  • 结合RecyclerView等

实战

创建数据源

1.非Room数据库

名称 使用场景
PageKeyedDataSource<Key, Value> 分页请求数据的场景
ItemKeyedDataSource<Key, Value> 以表的某个列为key,加载其后的N个数据
PositionalDataSource<T> 以数据源总数特定,根据指定位置请求数据的场景

2.Room数据库

如果是使用Room与Paging结合的方式呢

1
2
3
4
5
@Dao
interface CheeseDao {
@Query("SELECT * FROM Cheese ORDER BY name COLLATE NOCASE ASC")
fun allCheesedByName() : DataSource.Factory<Int, Cheese>
}

构建LiveData<PagedList>

1
2
3
val allCheeses = dao.allCheesesByName().toLiveData(Config) (pageSize = 30, 
enablePlaceholders = true,
maxSize = 200)

创建PagedListAdapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class CheeseAdapter : PagedListAdapter<Cheese, CheeseViewHolder>(diffCallback) {
override fun onBindViewHolder(holder: CheeseViewHolder, position: Int) {
holder.bindTo(getItem(position))
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CheeseViewHolder = CheeseViewHolder(parent)

companion object {
private val diffCallback = object : DiffUtil.ItemCallback<Cheese>() {
override fun areItemsTheSame(oldItem: Cheese, newItem: Cheese): Boolean =
oldItem.id == newItem.id

/**
* Note that in kotlin, == checking on data classes compares all contents, but in Java,
* typically you'll implement Object#equals, and use it to compare object contents.
*/
override fun areContentsTheSame(oldItem: Cheese, newItem: Cheese): Boolean =
oldItem == newItem
}
}
}
0%