The easiest way to see how all of this gets wired up is with a demonstration. For my example, I am working with the Vimeo API and making a call to the Videos endpoint.

Set up your RecyclerView first. I have chosen a LinearLayoutManger here and set up an ItemAnimator so that items will slide up when added and slide down when removed. Call setAdapter() to wire up the adapter to the RecyclerView. Then don’t forget to add an OnScrollListener.

layoutManager = new LinearLayoutManager(getActivity());

recyclerView.setLayoutManager(layoutManager); videosAdapter = new VideosAdapter();

recyclerView.setItemAnimator(new SlideInUpAnimator());

recyclerView.setAdapter(videosAdapter);



// Pagination

recyclerView.addOnScrollListener(recyclerViewOnScrollListener);

Let’s see what that OnScrollListener looks like. The responsibility of this listener is to load more items only if some conditions are satisfied. If you aren’t currently loading items and the last page hasn’t been reached then it checks against the current position that is in view to decide whether or not to load more items.

Ultimately you will be making two different API calls, an API call for the first page and an API call for the next page. Here is what the call to the first page looks like.

Call findVideosCall = vimeoService.findVideos(query,

sortByValue,

sortOrderValue,

currentPage,

PAGE_SIZE);

calls.add(findVideosCall);

findVideosCall.enqueue(findVideosFirstFetchCallback);

And here is the call to the next page.

Call findVideosCall = vimeoService.findVideos(query,

sortByValue,

sortOrderValue,

currentPage,

PAGE_SIZE);

calls.add(findVideosCall);

findVideosCall.enqueue(findVideosNextFetchCallback);

The call to get the first page has a callback method to handle the response. First you add all the videos from the response to the adapter. Then you check if the number of videos is greater than or equal to the PAGE_SIZE. If that is the case then add a loading item to the adapter otherwise set the flag isLastPage to true to prevent loading more items.

Similarly the call to get the next page has a callback method to handle the response. This time, when the response returns you want to remove the loading item before adding items to the adapter.

The complete example of how I set all of this up is in my repo

Loop — https://github.com/lawloretienne/Loop

The files you should focus on are :

Java files

https://github.com/lawloretienne/Loop/blob/master/app/src/main/java/com/etiennelawlor/loop/fragments/VideosFragment.java

https://github.com/lawloretienne/Loop/blob/master/app/src/main/java/com/etiennelawlor/loop/adapters/VideosAdapter.java

Xml files

https://github.com/lawloretienne/Loop/blob/master/app/src/main/res/layout/fragment_videos.xml

https://github.com/lawloretienne/Loop/blob/master/app/src/main/res/layout/video_row.xml