Home » Android » android – Implement Endless scroll on ListView

android – Implement Endless scroll on ListView

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

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