Skip to content

Instantly share code, notes, and snippets.

@ryancford
Created December 1, 2015 03:12
Show Gist options
  • Save ryancford/0b33f7da4626d573b755 to your computer and use it in GitHub Desktop.
Save ryancford/0b33f7da4626d573b755 to your computer and use it in GitHub Desktop.
Manual scroll detection.
private static final String DEBUG_TAG = "ScrollDirectionDebug";
private float y = -1;
private String mScrollDirection = "";
// OnTouchEvent is picked up by the child view that handles the event
// So we use dispatchTouchEvent which all events pass through
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "Action was " + event.toString());
int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case (MotionEvent.ACTION_DOWN):
y = event.getY();
return super.dispatchTouchEvent(event);
case (MotionEvent.ACTION_MOVE):
if (y >= event.getY()) {
mScrollDirection = "UP";
Log.d(DEBUG_TAG, "Scroll Direction: " + mScrollDirection);
this.onScrollUp();
} else if (y <= event.getY()) {
mScrollDirection = "DOWN";
Log.d(DEBUG_TAG, "Scroll Direction: " + mScrollDirection);
this.onScrollDown();
}
y = event.getY();
return super.dispatchTouchEvent(event);
case (MotionEvent.ACTION_UP):
y = -1;
mScrollDirection = "";
return super.dispatchTouchEvent(event);
}
return super.dispatchTouchEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment