Skip to content

Instantly share code, notes, and snippets.

@captrespect
Last active August 29, 2015 14:14
Show Gist options
  • Save captrespect/a58559adb82986ed26cd to your computer and use it in GitHub Desktop.
Save captrespect/a58559adb82986ed26cd to your computer and use it in GitHub Desktop.
Adds the total amount scrolled to a OnScrollListener for a ListView.
public abstract class OnTotalScrolledListener implements AbsListView.OnScrollListener {
private List<Integer> mHeights = new ArrayList<>();
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
View c = view.getChildAt(0);
if (c != null) {
if (mHeights.size() < firstVisibleItem + 1) {
mHeights.add(c.getHeight());
} else {
mHeights.set(firstVisibleItem, c.getHeight());
}
int heightSum = 0;
for (int i = 0; i < firstVisibleItem; i++) {
heightSum += mHeights.get(i);
}
float totalScrolled = -c.getTop() + heightSum;
onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount, totalScrolled);
}
else {
onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount, 0);
}
}
public abstract void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount, float totalScrolled);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment