interface RetrofitService {
@GET(“getContent”)
suspend fun getContentByTitle(@Query(“page”) page: Int, @Query(“title”) title: String): AgResponse<List>
}
定义PagingSource
class ContentPageSource(val title: String, private val mService: RetrofitService) : PagingSource<Int,Content>(){
override suspend fun load(params: LoadParams): LoadResult<Int, Content> {
val key=params.key?:0
val resource = exeRequest {
mService.getContentByTitle(key, title)
}
return if (resource is Resource.SuccessResource) {
resource.data?.let {
return LoadResult.Page(
it, null, if (key > 8) {//这里是假设获取了8页之后就没有下一页了
null
} else {
key + 1
}
)
}
LoadResult.Error(Exception())
} else {
LoadResult.Error(Exception())
}
}
ove