Last active
October 7, 2015 04:18
-
-
Save AlexNab/3104224 to your computer and use it in GitHub Desktop.
UninterceptableViewPager
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
| import android.content.Context; | |
| import android.support.v4.view.ViewPager; | |
| import android.util.AttributeSet; | |
| import android.view.GestureDetector; | |
| import android.view.GestureDetector.SimpleOnGestureListener; | |
| import android.view.MotionEvent; | |
| /** | |
| * Custom {@link ViewPager} implementation that will handle horizontal scroll events by himself. Default ViewPager | |
| * becomes hardly usable when it's nested into ScrollView based containers (such as ScrollView, ListView, etc.). It is | |
| * due to the fact that ScrollView based views will intercept any motion event with minimal (even 1px) vertical shift. | |
| * So to scroll ViewPager user will need to move his finger horizontally with zero vertical shift, which is obvious | |
| * quite irritating. | |
| */ | |
| public class UninterceptableViewPager extends ViewPager { | |
| // ----------------------------------------------------------------------- | |
| // | |
| // Constructor | |
| // | |
| // ----------------------------------------------------------------------- | |
| public UninterceptableViewPager(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| mGestureDetector = new GestureDetector(context, new YScrollDetector()); | |
| } | |
| // ----------------------------------------------------------------------- | |
| // | |
| // Fields | |
| // | |
| // ----------------------------------------------------------------------- | |
| private GestureDetector mGestureDetector; | |
| // ----------------------------------------------------------------------- | |
| // | |
| // Methods | |
| // | |
| // ----------------------------------------------------------------------- | |
| @Override | |
| public boolean onTouchEvent(MotionEvent event) { | |
| boolean isScrollY = mGestureDetector.onTouchEvent(event); | |
| getParent().requestDisallowInterceptTouchEvent(!isScrollY); | |
| return super.onTouchEvent(event); | |
| } | |
| // ----------------------------------------------------------------------- | |
| // | |
| // Inner Classes | |
| // | |
| // ----------------------------------------------------------------------- | |
| private class YScrollDetector extends SimpleOnGestureListener { | |
| /** | |
| * @return true - if we're scrolling in Y direction, false - in X direction. | |
| */ | |
| @Override | |
| public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
| return Math.abs(distanceY) > Math.abs(distanceX); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment