6

With the help of new Paging3 library, it has made us easy to insert items/separators in a recyclerview as shown in the google android codelabs tutorial https://developer.android.com/codelabs/android-paging#11 but how to get the logic of inserting items at every n position say as at every position 10 in the recyclerview.

Sample code

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

How to find the logic of adding an item on every 10th position in the above sample code

fun itemInsert(position: Int): Int {
    return if (position % 10 == 0) //each 10 position is separator 
        SEPARATOR else COMMON
}
Pranam Baruah
  • 61
  • 1
  • 3
  • 1
    I don't think it has anything to do with paging . you have to modify a `ItemDecorator`. you can modify `DividerItemDecoration` to work in this way . See https://stackoverflow.com/questions/28713231/android-add-divider-between-items-in-recyclerview. – ADM Jan 20 '21 at 08:05
  • @ADM Sorry sir I can understand but sir I didn't mean about ItemDecorator. Actually, I was asking about inserting an item at an interval of every 10th position with the help with the new android paging3 library only. With old tricks, I can achieve the same in the recyclerview but sir I need to implement with paging3 library only. – Pranam Baruah Jan 20 '21 at 08:16
  • Again not sure about paging lib . But this is just same as adding an item after each nth position in Array list . Something like [This](https://stackoverflow.com/a/40517854/4168607) have you tried it ? – ADM Jan 20 '21 at 08:50
  • 1
    @ADM Yes sir, the link you are providing is an old trick to achieve the same and with that I can do it in the recyclerview adapter but in paging3 library, the concept is a little bit different and the same your provided logic has to implement in the above sample code with before and after here it.insertSeparators { before, after ->} . The logic has to implement in the above sample code only. Paging3 library has lot more other advance features. – Pranam Baruah Jan 20 '21 at 09:15
  • Did u have the solution?, If yes please help me I face with this problem too @PranamBaruah – freeman Jan 29 '21 at 17:38
  • @PranamBaruah did you find any solution? – Anu Sep 17 '21 at 09:29
  • @Anu No, Not yet and still searching for the solution. – Pranam Baruah Sep 18 '21 at 12:32

1 Answers1

1

We just had the same problem and we decided to just manually count the index. We added: val index: Int in RepoItem constructor and the rest is:

val newResult: Flow<PagingData<UiModel>> = repository.getSearchResultStream(queryString)
    .map { pagingData ->
        var index = 0
        pagingData.map { UiModel.RepoItem(it, index++) }
    }
    .map {
        it.insertSeparators<UiModel.RepoItem, UiModel> { before, after ->
            if (before.index % 10) {
                Do something
            }
        }
    }

While this may work if you only append data, it more likely wont work if you set MAX_CACHE_SIZE.

Ahmed Ahmedov
  • 592
  • 12
  • 29