解决ViewPager内外层均可滑动时的滑动冲突

主要重写以下两个方法


    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
        ViewParent parent = getParent();
        // TODO Auto-generated method stub
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastMotionX = mInitialMotionX = x;
                mLastMotionY = mInitialMotionY = y;
                if (parent!=null) {
                    parent.requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int diffX = x - mLastMotionX;
                int diffY = y - mLastMotionY;
                //父容器可滑动条件
                if (mParentCanScroll) {
                    parent.requestDisallowInterceptTouchEvent(false);
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:break;
        }

        mLastMotionX = x;
        mLastMotionY = y;
        
        return super.dispatchTouchEvent(event);
    }



    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                //子容器可滑动条件
                if (mChildCanScroll) {
                    return false;
                }
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return super.onInterceptTouchEvent(event);
    }
requestDisallowInterceptTouchEvent(boolean)

返回true时 子View会递归处理所有父ViewGroup的onInterceptTouchEvent,让其跳过该回调。
返回false则相反

你可能感兴趣的:(解决ViewPager内外层均可滑动时的滑动冲突)