Last active
August 29, 2015 14:14
-
-
Save captrespect/a58559adb82986ed26cd to your computer and use it in GitHub Desktop.
Adds the total amount scrolled to a OnScrollListener for a 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
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