Endless Scrolling Listener – interface and class for an endless android listview

I am just too lazy to make an endless scrollview so I found one ready solution and I want to paste it here:

public class EndlessScrollListener implements AbsListView.OnScrollListener {

    private int visibleThreshold = 8;
    private int previousTotal = 0;
    private boolean loading = true;
    private OnEndReachedListener listener;

    public EndlessScrollListener() {
    }

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

    public EndlessScrollListener(OnEndReachedListener listener) {
        this.listener = listener;
    }

    public void setOnEndReachedListener(OnEndReachedListener listener) {
        this.listener = listener;
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (loading) {
            if (totalItemCount > previousTotal) {
                loading = false;
                previousTotal = totalItemCount;
            }
        }
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
            if (listener != null) {
                listener.onEndReached();
            }
            loading = true;
        }
    }

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

    public interface OnEndReachedListener {
        public void onEndReached();
    }
}

And how to call it:

public class StartActivity extends ActionBarActivity implements EndlessScrollListener.OnEndReachedListener
---------------------------------------------------------------

//I have a gridview gvImages and it sets the listener to be the activity.
EndlessScrollListener listener = new EndlessScrollListener(StartActivity.this);
gvImages.setOnScrollListener(listener);

---------------------------------------------------------------
//In your activity
@Override
    public void onEndReached() {
        Log.d("IS_LOADING_IMAGES", isLoadingImages + "");
        ......
    }

You may also like...