Skip to content

Instantly share code, notes, and snippets.

@Folyd
Created September 9, 2015 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Folyd/449878d07ed061f46bfd to your computer and use it in GitHub Desktop.
Save Folyd/449878d07ed061f46bfd to your computer and use it in GitHub Desktop.
A OnLastItemVisibleListener for ListView
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
public abstract class OnLastItemVisibleListener implements AbsListView.OnScrollListener {
private boolean mLastItemVisible = false;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
/**
* Check that the scrolling has stopped, and that the last item is
* visible.
*/
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
if (mLastItemVisible) {
onLastItemVisible();
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
/**
* Set whether the Last Item is Visible. lastVisibleItemIndex is a
* zero-based index, so we minus one totalItemCount to check.
*
*/
mLastItemVisible = (totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount /*- 1*/);
}
/**
* Called when the user has scrolled to the end of the list
*/
public abstract void onLastItemVisible();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment