Skip to content

Instantly share code, notes, and snippets.

@XinyueZ
Created August 19, 2014 09:22
Show Gist options
  • Select an option

  • Save XinyueZ/f1831db4a0273c5f259f to your computer and use it in GitHub Desktop.

Select an option

Save XinyueZ/f1831db4a0273c5f259f to your computer and use it in GitHub Desktop.
OneDirectionScrollView.
public class OneDirectionScrollView extends ScrollView {
private GestureDetector mGestureDetector;
private boolean mAllowTouch = true;
public OneDirectionScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode())
mGestureDetector = new GestureDetector(context, new YScrollDetector());
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (mGestureDetector != null)
mGestureDetector.onTouchEvent(ev);
if (mAllowTouch)
return super.onInterceptTouchEvent(ev);
return false;
}
// Return false if we're scrolling in the x direction
class YScrollDetector extends SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
if(Math.abs(distanceY) > Math.abs(distanceX)) {
mAllowTouch = true;
return true;
}
mAllowTouch = false;
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment