0

I'm trying to implement an endless scroll view (until the repository returns []), I've been reading and looking examples but they are for Room database, although I want to do it also with Room but first of all I'd like to have the paging with Retrofit my goal is to ask for a bunches of 10 items for instance but I'm not getting succeed, I've read that I would have to use PagedList but it's very new to me, could anyone of you provide me an example or a pseudo example of how to do it? My call to endpoint is like this :

https://api.example.com/pews/{string}/books?per_page=10&page=1

The problem is that my response is not returning in the json something related with the page, or maximum of pages, I just need to try until it returns empty list, also the thing is that I'm used to work with MVP instead of MVVM, but perhaps is easier to do it with MVVM, also I'm following the clean architecture UI - Presentation - Domain - Data.

I'd go for MVP first, since I can go straight forward but if it's easier with MVVP I just change the Presenter to ViewModel and do some changes and that's it.

the purpose of paging on my app is to have an endless scroll view, that every-time I'm arriving to the end it loads 10 items more.

StuartDTO
  • 783
  • 7
  • 26
  • 72

1 Answers1

-1

You need to implement an OnScrollListener, using the following variables:

  • visible Threshold – The minimum amount of items to have below your current scroll position, before loading more.
  • currentPage – The current page of data you have loaded
  • previousTotal – The total number of items in the dataset after the last load
  • loading – True if we are still waiting for the last set of data to load.

Override the onScroll method to achieve the expected behavior:

@Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading &amp;&amp; (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {

            // make your Retrofit call here using `currentPage + 1`
            loading = true;
        }
    }
Haroon
  • 538
  • 2
  • 11
  • What does it do with Paging? and MVP or MVVM? – StuartDTO May 10 '20 at 10:04
  • Doesn't matter MVP or MVVM, the onScrollListener is set on the recyclerView. Read documentation: https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.OnScrollListener – Haroon May 10 '20 at 10:07
  • 1
    The whole purpose of the paging archtecture componenent is to remove the necessity for custom, untested logic, with standardised and flexible mechansims for paging. The paging library provides all relevant callbacks when to load items, negating the need for this. However it just needs to be implemented correctly - which the OP's issue, albeit a poor question. – Mark May 10 '20 at 11:22