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

2018-06-23  本文已影响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);
    }
上一篇 下一篇

猜你喜欢

热点阅读