AsyncListUtil 可以异步加载数据,并根据列表滑动会自动分页加载,数据加载完成会回调主线程更新UI,并且可以在Item离屏后实现Recycle
缺陷
refreshData()
方法需要指定数据总条数,只能在调用AsyncListUtil.refresh()
方法时指定,无法动态设置数据总数,如果是接口获取数据,必须已知数据总条数;fillData(data: Array<T>, startPosition: Int, itemCount: Int)
方法分页加载给出了startPosition和itemCount,也就限定了分页一定是有页码的,可以明确根据startPosition获取下一页数据,无法根据时间戳或者其他标识动态获取下一页数据;- AsyncListUtil在初始化时指定了分页大小,也就限定了分页加载大小,无法根据浏览情况,动态设置分页加载大小;
- 调用
AsyncListUtil.refresh()
后默认还在当前分页index,如果想要回到第一页,需要自己维护逻辑;
注意
AsyncListUtil.refresh()
方法只是刷新当前页面可见数据,并不会回到首页;
适用场景
- 异步加载数据库数据,可以已知总条数,并能分页加载;
- 分页接口已知数据总数,分页大小固定;
示例
class AsyncListUtilActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recyclerview)
supportActionBar!!.title = "RecyclerView"
initView()
}
private fun initView() {
val adapter = AsyncAdapter()
recyclerView?.apply {
this.layoutManager = LinearLayoutManager(this.context, RecyclerView.VERTICAL, false)
val horizontalItemDecoration = DividerItemDecoration(this.context, DividerItemDecoration.HORIZONTAL)
horizontalItemDecoration.setDrawable(this.context.resources.getDrawable(android.R.drawable.divider_horizontal_textfield))
this.addItemDecoration(horizontalItemDecoration)
this.adapter = adapter
addOnScrollListener(ScrollListener