[英]Load more data on click of an item in Paging 3 library
我正在使用分頁 3 庫,我想通過單擊回收器中的按鈕來加載下一頁項目。
//for first case it will be null, then we can pass some default value, in our case it's 1
val pageNumber = params.key ?: DEFAULT_PAGE_INDEX
PAGE_NUMBER = DEFAULT_PAGE_INDEX
return try {
val response = tasksApi.getTasks(
token = "Bearer " + NicoPrefs.getSSOAccessToken(),
status = pagingTasksInput.status,
type = pagingTasksInput.type,
activity = pagingTasksInput.activity,
sort = pagingTasksInput.sort,
sortBy = pagingTasksInput.sortBy,
pageNumber = if (pagingTasksInput.pageNumber > 0) pagingTasksInput.pageNumber else pageNumber,
pageLimit = pagingTasksInput.pageLimit,
assignee = pagingTasksInput.assignee
)
val data = pagedResponse?.result
LoadResult.Page(
data.orEmpty(),
prevKey = if (pageNumber == DEFAULT_PAGE_INDEX) null else pageNumber - 1,
nextKey = null
)
} catch (exception: IOException) {
return LoadResult.Error(exception)
} catch (exception: HttpException) {
return LoadResult.Error(exception)
}
}
我添加了頁腳
map {
it.insertFooterItem(TaskDto(footer = "FOOTER", description = "Load more"))
}
當我單擊“加載更多”時,更新頁碼並進行 API 調用(在我的情況下,頁面限制為 50)
viewLifecycleOwner.lifecycleScope.launch {
testViewModel.getPagingTasks(getAssignedTasks)
.collectLatest { response ->
Timber.d("paging item = ${assignedTasksPagingAdapter?.snapshot()}")
assignedTasksPagingAdapter?.submitData(
response
)
}
}
我必須將接下來的 50 個項目附加到回收站視圖中現有的 50 個項目。 我們如何做到這一點?
Paging 中的頁面獲取由通過對演示者(例如 PagingDataAdapter)調用.get(index)
的項目訪問觸發。 如果您在訪問項目時需要避免頁面獲取,您可以調用.peek(index)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.