Paging正确使用方法

  • Pager不应该重复创建,但是持有Pager本身没有什么意义,通常持有flow即可
  • 如果需要刷新页面内容(如加载失败重试)流程如下
    1. 调用PagingSource中的invalidate方法
    2. Pager会自动调用PagingSource.getRefreshKey()来覆盖初始化时的initialKey
    3. Pager通过传入的最后一个参数pagingSourceFactory重新创建了一个PagingSource
    4. 最后更新ViewModel中你持有的PagingSource引用
  • 如果需要支持跳页,修改以下配置
    1. 创建Pager时,PagingConfig.jumpThreshold 给一个值,一般来说pageSize * 2 即可
    2. PagingSource 重写 jumpingSupported,返回true
    3. PagingSource 重写 getRefreshKey,返回目标跳转页数(用于创建下一个新的PagingSource)
    4. PagingSource 重写 load 时 LoadResult.Page 需要返回 itemsBefore itemsAfter