30

I would like to implement endless scroll on an android ListView component, that will add new items to the bottom of the listview when scrolled to the last element in the list.

Ajibola
  • 1,218
  • 16
  • 28
ManiTeja
  • 849
  • 1
  • 9
  • 13

2 Answers2

35

implement the onscrolllistener()

public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
}

public void onScrollStateChanged(AbsListView view, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
 }

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work for load more date! ***/
        if(!isLoading){
             isLoading = true;
             loadMoreDAta();
        }
    }
}

as you implement and add this listener to your listview this will detect for the end of listview as scroll position was at end of list just get more data. And during the loading data you need one flag to load data at once when the scroll position goes to end. So if data is loading and during that time you scroll up then scroll down then it will not get more data for dupplication.

Pratik
  • 30,639
  • 18
  • 84
  • 159
  • hai, can u tell me how to add data when the scroll reaches to the bottom in gridview .in page wise.i am trying this for 2 weeks.can u help me – ManiTeja Sep 25 '12 at 13:15
  • 1
    add data into your list or arraylist or array which you have pass to your adapter for the gridview and call the notifyDataSetChanged() of adapter after data added to refresh gridview. – Pratik Sep 25 '12 at 13:17
  • to avoid the isScrollCompleted being called multiple times i have adjusted the method to use a threshold, i.e. only if that number of items are visible. public void onScrollStateChanged(AbsListView view, int scrollState) { this.currentScrollState = scrollState; if ((jobAdapter.getCount() - (currentFirstVisibleItem + currentVisibleItemCount)) <= threshold) { this.isScrollCompleted(); } } – Ajibola Mar 01 '15 at 20:28
  • @Pratik how to load data from server once i reach to the end of list ? – Erum Aug 19 '15 at 11:47
  • @Erum I have write loadMoreDAta(); in isScrollCompleted() you need to create this method and get the data from server. You just need to manage the pagination with passing param in your API. – Pratik Aug 20 '15 at 06:28
  • @Pratik i have written method that will bring data from server but i m not getting how to pass pagination ?? i m dealing like this first bring only 6 items then again 6+6 then again 6+6+6 how will i do this ? / – Erum Aug 20 '15 at 06:29
  • @Erum I don't know your were passing any param or not in your api but You need to pass 1 param as Page like Page = 1 for first time, on next call Page = 2 and so on... Now at your server side script get that Page and as per that get that value from table. – Pratik Aug 20 '15 at 09:10
  • @Pratik if i have to show only 5 data in gridview then will i load only 5 data from server by passing limit to server -http://abccom/api/UserAll/param/345/login_id/343/page/1/limit/10000 – Erum Aug 20 '15 at 10:53
9

Try with OnScrollListener Just add the items to your ListView in your

public void onScroll(AbsListView view,
    int firstVisible, int visibleCount, int totalCount) {

    // Add the items

    }
}

And, setting your adapter.notifyDataSetChanged() will display the added items in your ListView Here i give you some example that how to integrate the onScrollListener with your ListView

  1. Android Endless List

  2. Endless Scrolling Listview

  3. Endless ListView

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173