Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView setOnScrollListener

I'm having problem with my setOnScrollListener. It just keeps calling my asynctask whenever I scroll to the bottom of the listview. How do I set the setOnScrollListener to load only once I reach the bottom.

listview.setAdapter(adapter);
mProgressDialog.dismiss();

listview.setOnScrollListener(new OnScrollListener() {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) { 
        // TODO Auto-generated method stub
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        int lastInScreen = firstVisibleItem + visibleItemCount;
        if (lastInScreen == totalItemCount) {
            new loadmore().execute();
        } else {
        }
    }
);
like image 704
KC Chai Avatar asked Jul 04 '13 15:07

KC Chai


People also ask

Are there any real world examples of Android listview in Java?

These are the top rated real world Java examples of android.content.ListView.setOnItemClickListener extracted from open source projects. You can rate examples to help us improve the quality of examples.

What is onscrolllistener in recyclerview?

An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event has occurred on that RecyclerView. Callback method to be invoked when RecyclerView's scroll state changes. Callback method to be invoked when the RecyclerView has been scrolled. Callback method to be invoked when RecyclerView's scroll state changes.

When is the callback for scroll called?

This will be called after the scroll has completed. This callback will also be called if visible item range changes after a layout calculation. In that case, dx and dy will be 0. RecyclerView: The RecyclerView which scrolled. int: The amount of horizontal scroll. int: The amount of vertical scroll.


1 Answers

OnScroll method is called whenever you scroll down the list view, so the best bet for you to is to use some kind of padding, like the one implemented here.

public class EndlessScrollListener implements OnScrollListener 

    private int visibleThreshold = 5;
    private int currentPage = 0;
    private int previousTotal = 0;
    private boolean loading = true;

    public EndlessScrollListener() {
    }
    public EndlessScrollListener(int visibleThreshold) {
        this.visibleThreshold = visibleThreshold;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
                currentPage++;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            // I load the next page of gigs using a background task,
            // but you can call any function here.
            new LoadGigsTask().execute(currentPage + 1);
            loading = true;
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
    }
}

visibleThreshold – The minimum amount of items to have below your current scroll position, before loading more.

currentPage – The current page of data you have loaded

previousTotal – The total number of items in the dataset after the last load

loading – True if we are still waiting for the last set of data to load.

like image 124
Mushtaq Jameel Avatar answered Oct 21 '22 14:10

Mushtaq Jameel