Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Helper class for calculating relative scroll offsets in a ListView or GridView by tracking the position of child views.
import android.util.SparseArray;
import android.widget.AbsListView;
/**
* Helper class for calculating relative scroll offsets in a ListView or GridView by tracking the
* position of child views.
*/
public class ListViewScrollTracker {
private AbsListView mListView;
private SparseArray<Integer> mPositions;
public ListViewScrollTracker(final AbsListView listView){
mListView = listView;
}
/**
* Call from an AbsListView.OnScrollListener to calculate the incremental offset (change in scroll offset
* since the last calculation).
*
* @param firstVisiblePosition First visible item position in the list.
* @param visibleItemCount Number of visible items in the list.
* @return The incremental offset, or 0 if it wasn't possible to calculate the offset.
*/
public int calculateIncrementalOffset(final int firstVisiblePosition, final int visibleItemCount){
// Remember previous positions, if any
SparseArray<Integer> previousPositions = mPositions;
// Store new positions
mPositions = new SparseArray<Integer>();
for(int i = 0; i < visibleItemCount; i++){
mPositions.put(firstVisiblePosition + i, mListView.getChildAt(i).getTop());
}
if(previousPositions != null){
// Find position which exists in both mPositions and previousPositions, then return the difference
// of the new and old Y values.
for(int i = 0; i < previousPositions.size(); i++){
int position = previousPositions.keyAt(i);
int previousTop = previousPositions.get(position);
Integer newTop = mPositions.get(position);
if(newTop != null){
return newTop - previousTop;
}
}
}
return 0; // No view's position was in both previousPositions and mPositions
}
public void clear(){
mPositions = null;
}
}
@cnnrhill

This comment has been minimized.

Copy link
Owner Author

@cnnrhill cnnrhill commented Sep 24, 2013

And then, in your listener:

@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    int incrementalOffset = mScrollTracker.calculateIncrementalOffset(firstVisibleItem, visibleItemCount);
}
@qluan

This comment has been minimized.

Copy link

@qluan qluan commented Sep 8, 2015

perfect

@seyoung-hyun

This comment has been minimized.

Copy link

@seyoung-hyun seyoung-hyun commented Feb 12, 2018

@cnnrhill
Thanks your code :)
Can I use your code in my commercial app?
Please let me know how I can use the code for commercial distribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment