Created
August 19, 2014 09:22
-
-
Save XinyueZ/f1831db4a0273c5f259f to your computer and use it in GitHub Desktop.
OneDirectionScrollView.
This file contains hidden or 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 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