0

Hello guys i fetched data from firebase in recyclerview. For better understanding my question please visit my previous question. How do i add the show more functionality to getting firebase database?

This is my previous question. For this i found some logic(it might be wrong). If i set query ,

Query sortPost=PostRef.orderByChild("count").limitToFirst(firstTenPost);

and i set the Scrolllistner to detect scroll reach at bottom

postList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);firstTwoPost=2;
                Toast.makeText(MainActivity.this, "Last", Toast.LENGTH_LONG).show();

//So here i want to add +10 items with show more in recyclerview
                    }
                }
            });
tailor
  • 373
  • 4
  • 19

1 Answers1

0

As per your requirement, you won't need to increase the limit of limitOfFirst().

You should have to try with startAt() method. Refer below code for better understanding.

On first load:

Code should be,

Query sortPost=PostRef.orderByChild("count").limitToFirst(firstTenPost);

and on response of this query, find the last key from child records using iterators.

You can get last key from reponse snapshot using below loop,

for(DataSnapshot childSnap : dataSnapshot.getChindren()){
   lastKey = childSnap.getKey();
}

And On load more use below query,

String lastKey = "Last Fetched Key From Earlier Response";
Query sortPost=PostRef.orderByChild("count").startAt(lastKey).limitToFirst(firstTenPost);

And on response of this query, update lastKey variable with last obtains record-key.

Note: On loadmore response you should have to skip the first value every time to avoid duplicattion of object.

DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
  • how i find lastkey in onbindviewholder and How i come out with lastkey from onbindviewholder – tailor Jun 12 '19 at 16:29
  • for(DataSnapshot childSnap : dataSnapshot.getChindren()){ lastKey = childSnap.getKey(); } I cant use this loop until i used valueevenelistner. Please see code i never used listner to get the value – tailor Jun 12 '19 at 16:54
  • I think your startAt() call here is slightly off. You should be passing two arguments (the value for orderByChild AND then the lastKey) – Kato Jun 13 '19 at 00:05
  • @Kato which two arguments ? – tailor Jun 13 '19 at 11:43
  • The value for the orderByChild and the lastKey. [reference doc](https://firebase.google.com/docs/reference/android/com/google/firebase/database/Query.html#endAt(java.lang.String,%20java.lang.String)) – Kato Jun 13 '19 at 17:54
  • I am not using Listner and Arraylist, but i used a FirebaseRecyclerOption , so how can i find lastKey from firebase – tailor Jun 18 '19 at 17:29