title: Firebase to load more items on scroll (Android)
body: <p>I'm trying to make a chat application, I use Firebase as my backend,
  I'm populating my listview onCreate() with the last 10 messages sent,
  and I just want that when the user scrolls up ^ it will load 10 more every time.</p>
  
  <p>I got all working, the problem is that the Firebase loads <strong>all</strong> the data every time, and not increasing by 10 the existing data.</p>
  
  <p>For example: it loads 10 messages at startup, and when I scroll up it loads 20 messages, and then it loads 30 messages, instead of just loading 10 more at a time.</p>
  
  <p>This is the code:</p>
  
  <pre><code>//Listview to find when user is scrolling and reach the top i.e firstVisibleItem == 0
  
  messagesList.setOnScrollListener(new AbsListView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(AbsListView absListView, int i) {
  
      }
  
      @Override
      public void onScroll(AbsListView view, int firstVisibleItem,
          int visibleItemCount, int totalItemCount) {
  
          if (firstVisibleItem == 0 &amp;&amp; !loading) {
              msgNum += 10;
              setMessagesList(msgNum); //your load more function
          }
      }
  });
  
  //Firebase to load more on scroll code:
  public void setMessagesList(int msgNum) {
  
      MessegesRef2 = database.getReference("chatsMessages").child(chatID);
      messageValueListener = MessegesRef2.orderByChild("creationDate").limitToLast(msgNum).addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              loading = true;
              for (DataSnapshot data: dataSnapshot.getChildren()) {
  
                  ChatMessage chatMessage = data.getValue(ChatMessage.class);
  
                  chatMessageAdapter.add(chatMessage);
                  chatMessageAdapter.notifyDataSetChanged();
              }
              loading = false;
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
          }
      });
  }
  </code></pre>