Pagination in List RecyclerView using Youtube Data API Android

Fetching Comments ...

Use pagination in android RecyclerView , ListView,GridView or any other list.

What is pagination?

Pagination means load data in some chunks called as pagination. For example load 100 items in 10 parts having 10 item each, called as pagination.

Why we need pagination?

If you are loading 10,000 items from Web API or local database then it will take more time to fetch data from Web API and display them in List. So its better to display first 10 items, and if user want to view more then load 10 more and so on. This will increase load time of data and performance of app too.

In this example I’m using Youtube Data API, you can use any API which you preffer. And I’m using RecyclerView to display items, you can use ListView or GridView too.

Main Idea behind this Logic :

In Youtube API we get token (String) of next list, so we will pass that token while requesting next data call. But first time we will pass empty value as token so it will return latest items.

But if you want to use Pagination in your own web API then ask your Backend developer to add 2 request parameters “start” and “limit”. And return “last_index” along with data. “start” parameter will be used to fetch data from “start” index and “limit” parameter will be used to max number of data.

For Example :

if you pass 0 as “start” and 10 as “limit” parameter then API will return data from 0 to 10 items. And now in next call pass “last_index + 1” as start parameter. So now you will pass 11 as “start” and 10 as “limit” and you will get data from 11 to 20 and so on.

Now when you get data from Web API then add that data to adapter of RecyclerView, ListView or GridView. You can also replace data of Adapter if you want to display only new items.

code of MainActivity.java

code of activity_main.xml

code of APIService.java

code of YoutubeApi.java

code of YoutubeResponse.java

code of VideosAdapter.java

Looking for full source code of this project then fork from below link

star this repo fork this repo

Download demo APK from below link

Download APK

Share This:

LearnPainLess On Instagram

Get The Best Of All Hands Delivered To Your Inbox

Subscribe to our newsletter and stay updated.