美文网首页安卓知识精选案例
解决ViewPager内外层均可滑动时的滑动冲突

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

作者: litao0621 | 来源:发表于2016-03-28 13:29 被阅读458次

    主要重写以下两个方法

    
        @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内外层均可滑动时的滑动冲突

        本文链接:https://www.haomeiwen.com/subject/blpjlttx.html