3

I want to load data when ScrollView reach at the threshold. First, I want to show last 20 rows from database and show in ListView then after using SwipeRefreshLayout load another 20 rows get from database and show in ListView and so on...

Thank you

Misha Akopov
  • 12,241
  • 27
  • 68
  • 82
  • I'm not sure on ListView but in RecyclerView you have to overrride `mRecyclerView.setOnScrollListener(new OnScrollListener() {..}`, inside that there is `onScrollStateChanged` which you implement your logic. Like if `(totalItem==20)&&(totalitem==currentVisibleItem+previousSeenItems"...` , then `loadMoreFromDB() ...` – Jemshit Aug 05 '15 at 08:26
  • have a look into this http://stackoverflow.com/questions/5065339/android-dynamically-load-listview-at-scroll-end – King of Masses Aug 05 '15 at 09:12

2 Answers2

2

What you are searching for is called EndlessScrollListener. You will have to extend the class EndlessScrollListener and implement the onLoadMore method.

public class MyEndlessScrollListener extends EndlessScrollListener {

   @Override
   public void onLoadMore(int page, int totalItemsCount) {
        loadData(page);  
   }
}

And set the listener on the listView :

listView.setOnScrollListener(new EndlessScrollListener());

You can use the other constructors of the EndlessScrollListener if you want.

Dimitri
  • 8,122
  • 19
  • 71
  • 128
0

This is what I used to load more data at the end of the listview

 listview.setOnScrollListener(new OnScrollListener(){

        @Override
        public void onScroll(AbsListView view,
        int firstVisibleItem, int visibleItemCount,
        int totalItemCount) {
        //Algorithm to check if the last item is visible or not
        final int lastItem = firstVisibleItem + visibleItemCount;
        if(lastItem == totalItemCount){                 
        // you have reached end of list, load more data probably call your database to load more data            
        }

        @Override
        public void onScrollStateChanged(AbsListView view,int scrollState) {
        //blank, not using this
        }
        });

Hope this link may help you

Community
  • 1
  • 1
King of Masses
  • 18,405
  • 4
  • 60
  • 77