Updating Paging 3 alpha to stable cause indexing issue Android

I noticed you filed a bug here: https://issuetracker.google.com/204328119, but I also wanted to update this answer for other future people reading this issue on SO.

The core issue is that you are starting collection on Paging before ViewPager is ready to start binding this items. You should use lifecycleScope.launchWhenCreated instead of lifecycleScope.launc to fix this:

lifecycleScope.launchWhenCreated {
  viewModel.dataList.collectLatest {
    adapter.submitData(it)
  }
}

The other issue I noticed (which would also workaround this), is that you have placeholders enabled, but aren’t passing in itemsBefore or itemsAfter in LoadResult.Page. Enabling placeholders and having a static count would also give your view a list size to bind, but since you pass COUNT_UNDEFINED, Paging is not able to pad the list with null placeholders properly since it has no idea how many to add.

Leave a Comment