0

I am making a sample android app which shows all data present inside Firebase Database with the help of RecyclerView. It is working as I expected. Issue Is If I add More Items In Database As Time Passes. It would still get all data and show in recyclerView. It would be definitely slow (Due to internet and slow performance Of Devices) and would take some time to show data on recyclerView.

I heard of a way to resolve this by making them small chunks and loading another one when user scrolls further. This Process is known as pagination. But In case Of Firebase Database, there is no direct way to paginate data in recyclerView. I have also searched for this issue on internet but none of them work.

Has anyone idea how to paginate firebase database data in recyclerView?.

Pagination Can Be Understood By This Sample Video

EzLo
  • 13,780
  • 10
  • 33
  • 38
  • Welcome to SO Anuj. Unfortunately pagination is a huge topic so this sort of question is too broad and I'm flagging to close. Your question is better suited to a forum or subreddit. SO is designed for solving specific programming issues in Q&A format. – James Jones Aug 24 '18 at 05:23
  • If you plan to switch to Cloud Firestore, **[this](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo Aug 24 '18 at 12:09

1 Answers1

2

to do so you need to define two type of lists. One for major list which have all the elements and second one which have limit data.

private Query getQuery(String query) {
    Query query;

    if (recentLastItem == null) // recent last item is the any type of object which you have stored in the firebase
        query = FirebaseDatabase.getInstance().getReference()
                .child(your saved data location)
                .orderByKey()
                .limitToLast(no of elements per time you want);
             // this if call when you try first time to get the items from firebase 
    else
        query = FirebaseDatabase.getInstance().getReference()
                .child(your save data location)
                .orderByKey()
                .startAt(recentLastItem)
                .limitToFirst(no of items);
             // this else part run when you try to get next no of items
    return query;
}

get this query object and call value Listner on it. and one More thing you need to call getQuery() method whenever user reach the end screen of the device.

And do not forgot to update the value of last element from your list after seting the recyclerView.setAdapter(adapter) by calling recentLastItem = yourListName.get(yourListName.size() -1)

Faiizii Awan
  • 1,615
  • 1
  • 13
  • 28