美文网首页
解决自定义view与viewgroup事件冲突

解决自定义view与viewgroup事件冲突

作者: 码农朱同学 | 来源:发表于2018-06-23 18:24 被阅读0次

    刚测试提出的bug, 当手指在图表上时,没法上线滑动。显然是事件传递的问题,就此解决,并下下过程。
    我自定义的这个类QuantDKLinePlusChart

           setOnTouchListener((v, event) -> {
    
                if (lineType == 1) {
                    isFirstOpen = false;
    
                    if (event.getAction() == MotionEvent.ACTION_DOWN) {
                        isShowLastLine = false;
    
                        if (null != getParent())
                            getParent().requestDisallowInterceptTouchEvent(false);
                    } else if (event.getAction() == MotionEvent.ACTION_UP) {
                        isShowLastLine = false;
    
                        if (null != getParent())
                            getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
    
                return false;
    
            });
        }
    
        @Override
        public boolean onDown(MotionEvent e) {
    
            cleanCursor();
            if (null != getParent())
                getParent().requestDisallowInterceptTouchEvent(false);
            return true;
        }
    
        @Override
        public void onShowPress(MotionEvent e) {
            cleanCursor();
            if (null != getParent())
                getParent().requestDisallowInterceptTouchEvent(false);
        }
    
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            if (null != getParent())
                getParent().requestDisallowInterceptTouchEvent(true);
    
            this.currentTouchPos = getCurrentTouchPosByCoordinate(e.getX());
    
            if (currentTouchPos <= dataList1.size() - 1 && currentTouchPos >= 0) {
    
                isShowCursor = true;
                drawCursor(e.getX());
            }
            if (currentTouchPos > dataList1.size() - 1) {
                currentTouchPos = dataList1.size() - 1;
            }
            if (MotionEvent.ACTION_UP == e.getAction()
                    || MotionEvent.ACTION_CANCEL == e.getAction()) {
    
                cleanCursor();
                if (null != getParent())
                    getParent().requestDisallowInterceptTouchEvent(false);
            }
    
            return false;
        }
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (null != getParent())
                getParent().requestDisallowInterceptTouchEvent(true);
    
            this.currentTouchPos = getCurrentTouchPosByCoordinate(e2.getX());
    
            if (currentTouchPos <= dataList1.size() - 1 && currentTouchPos >= 0) {
    
                isShowCursor = true;
                drawCursor(e2.getX());
            }
            if (currentTouchPos > dataList1.size() - 1) {
                currentTouchPos = dataList1.size() - 1;
            }
            if (MotionEvent.ACTION_UP == e2.getAction()
                    || MotionEvent.ACTION_CANCEL == e2.getAction()) {
    
                cleanCursor();
                if (null != getParent())
                    getParent().requestDisallowInterceptTouchEvent(false);
            }
    
            return false;
        }
    
        @Override
        public void onLongPress(MotionEvent e) {
            cleanCursor();
            if (isLineEmpty) {
                return;
            }
    
            if (null != getParent())
                getParent().requestDisallowInterceptTouchEvent(false);
        }
    

    相关文章

      网友评论

          本文标题:解决自定义view与viewgroup事件冲突

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