Last active
June 19, 2017 11:46
-
-
Save s1ntoneli/f713cb19a2d5cda0468d9c9c71d5df64 to your computer and use it in GitHub Desktop.
解决 ScrollView 中嵌套 ViewPager 的滑动冲突问题。
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.MotionEvent; | |
| import android.view.ViewConfiguration; | |
| public class SelfishViewPager extends ViewPager { | |
| int mScaledTouchSlop = 0; | |
| public SelfishViewPager(Context context) { | |
| super(context); | |
| } | |
| public SelfishViewPager(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); | |
| } | |
| private int downX; | |
| private boolean isSelfish = false; | |
| @Override | |
| public boolean dispatchTouchEvent(MotionEvent ev) { | |
| switch (ev.getAction()) { | |
| case MotionEvent.ACTION_DOWN: | |
| isSelfish = false; | |
| downX = (int) ev.getX(); | |
| break; | |
| case MotionEvent.ACTION_MOVE: | |
| if (!isSelfish && ev.getX() - downX > mScaledTouchSlop) { | |
| requestParentDisallowInterceptTouchEvent(); | |
| isSelfish = true; | |
| if (mSelfishListener != null) { | |
| mSelfishListener.onSelfished(); | |
| } | |
| } | |
| break; | |
| case MotionEvent.ACTION_UP: | |
| case MotionEvent.ACTION_CANCEL: | |
| isSelfish = false; | |
| break; | |
| } | |
| return super.dispatchTouchEvent(ev); | |
| } | |
| private void requestParentDisallowInterceptTouchEvent() { | |
| if (getParent() != null) { | |
| getParent().requestDisallowInterceptTouchEvent(true); | |
| } | |
| } | |
| public interface SelfishListener { | |
| void onSelfished(); | |
| } | |
| private SelfishListener mSelfishListener; | |
| public void setSelfishListener(SelfishListener listener) { | |
| mSelfishListener = listener; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment