Skip to content Skip to sidebar Skip to footer

Load Next And Previous Page On Listview Scroll In Android

I am creating a ListView in which I am fetching news from server. And I am getting data page wise i.e. at one time I got one page with 10 items. On down scroll I am hitting that w

Solution 1:

You need to validate the last page after each hit response. Better way to do it on server site . But you can also manage it at client end . below is an example .

privateboolean isLastPage;
privateint PER_PAGE_COUNT=10;
//@params size- size of data which you get form server each time hit i shoule <= PER_PAGE_COUNTpublicvoidvalidateLastpage(int size) {
    if(size==0){
        isLastPage=true;
    }else {
        if (size % PER_PAGE_COUNT == 0) {
            isLastPage = false;
        } else {
            isLastPage = true;
        }
    }
}

Now check for isLastPage on each hit. This will stop your extra network called after no more data at server site. Call validateLastpage(int size) after each response with new data size . On each pagination check for last page .

publicvoidonScroll(AbsListView view, int firstVisibleItem,
                     int visibleItemCount, int totalItemCount){
    Log.e(TAG, "onScroll: "+firstVisibleItem +" "+visibleItemCount+" "+totalItemCount );
    if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
        if(!isLastPage)
        callNewsApi((Integer.parseInt(currentPage) + 1) + "");
    }
}

Post a Comment for "Load Next And Previous Page On Listview Scroll In Android"