在新的 Paging3 库的帮助下,它使我们可以轻松地在 recyclerview 中插入项目/分隔符,如 google android codelabs 教程https://developer.android.com/codelabs/android-paging#11所示,但是如何获取在 recyclerview 中每 n 个位置插入项目的逻辑是在每个位置 10 处插入项目。
示例代码
fun searchRepo(queryString: String): Flow<PagingData<UiModel>> {
val lastResult = currentSearchResult
if (queryString == currentQueryValue && lastResult != null) {
return lastResult
}
currentQueryValue = queryString
val newResult: Flow<PagingData<UiModel>> = repository.getSearchResultStream(queryString)
.map { pagingData -> pagingData.map { UiModel.RepoItem(it) } }
.map {
it.insertSeparators<UiModel.RepoItem, UiModel> { before, after ->
if (after == null) {
// we're at the end of the list
return@insertSeparators null
} …Run Code Online (Sandbox Code Playgroud)