Created
September 9, 2015 07:05
-
-
Save Folyd/449878d07ed061f46bfd to your computer and use it in GitHub Desktop.
A OnLastItemVisibleListener for ListView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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