如何使用 Paging3 库在 recyclerview 中的每个 n 位置添加分隔符?

问题描述

在新的 Paging3 库的帮助下,它使我们可以轻松地在 recyclerview 中插入项目/分隔符,如 google android codelabs 教程 https://developer.android.com/codelabs/android-paging#11 所示,但是如何获得在每个 n 位置插入项目的逻辑比如说在 recyclerview 中的每个位置 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
                }

                if (before == null) {
                    // we're at the beginning of the list
                    return@insertSeparators UiModel.SeparatorItem("${after.roundedStarCount}0.000+ stars")
                }
                // check between 2 items
                if (before.roundedStarCount > after.roundedStarCount) {
                    if (after.roundedStarCount >= 1) {
                        UiModel.SeparatorItem("${after.roundedStarCount}0.000+ stars")
                    } else {
                        UiModel.SeparatorItem("< 10.000+ stars")
                    }
                } else {
                    // no separator
                    null
                }
            }
        }
        .cachedIn(viewmodelScope)
currentSearchResult = newResult
return newResult

如何在上面的示例代码中找到每10个位置添加一个item的逻辑

fun itemInsert(position: Int): Int {
    return if (position % 10 == 0) //each 10 position is separator 
        SEParaTOR else COMMON
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)