1

Today I have implemented paging library in my app that uses TMDb api. Everything work nice, but I'm struggling with displaying current page.

In DataSource class I have value:

val currentPage = MutableLiveData<Page>()   // Page => currentPage, totalPages

that is updated every "loadAfter" function.

This MutableLiveData is observed in ViewModel and then displayed in Activity. When user scroll down, the number of current page increase, but when user scroll up, the number stay the same.

Anyone have an idea how to notify scrolling up?

Corrot
  • 23
  • 4
  • Are you using RecyclerView or something else ? – Hossam Eldeen Onsy Jul 06 '19 at 21:04
  • @HossamEldeenOnsy RecyclerView with PagedListAdapter. – Corrot Jul 06 '19 at 21:20
  • RecyclerView has onScrollListener which is entered when it's scrolling like in here https://stackoverflow.com/a/29026504/7586266 and since you get currentFirstVisible you can use it to check which page you are in – Hossam Eldeen Onsy Jul 06 '19 at 21:27
  • @HossamEldeenOnsy Thanks, that's a good point. Now I need to assign somehow page number to every item from my response List , becouse from api GET call i get: `data class MovieResponse( val page: Int, val total_pages: Int, val results: List )` – Corrot Jul 06 '19 at 22:05
  • Ummm just an idea , mostly position is a part of the bigger picture so you can simply divide the current position by the list current size and you will get the page you are wish – Hossam Eldeen Onsy Jul 06 '19 at 22:06
  • I'll post it as an answer for other developers who would face the same issue to check it directly as it's indeed something that seems interesting for scrolling in particular with pagination :) – Hossam Eldeen Onsy Jul 06 '19 at 22:09

1 Answers1

1

RecyclerView has onScrollListener which is entered when it's scrolling like in here and since you get currentFirstVisible you can use it to check which page you are in.

As for the getting the page that you have scrolled to mostly position is a part of the bigger picture so you can simply divide the currentFirstVisible position by the list current size and you will get the page you are wish

Ref: RecyclerView.OnScrollListener-Docs