MENU

列表中手势冲突问题

November 7, 2020 • 笔记

纵向布局嵌套横向布局,且子布局中存在手势控制。解决冲突的步骤。

1. 排除多点触控

horizentalChildView.setOnTouchListener { _, event ->
            //getPointerCount()在某种情况下可能会出现一直返回1的情况
            if (event.pointerCount == 2) {
                groupView.requestDisallowInterceptTouchEvent(true)
            } else {
                groupView.requestDisallowInterceptTouchEvent(false)
            }
            false
        }
  1. 判断滑动方向
        //four variable to save position
    private float startX, startY, moveEndX, moveEndY;
    ...{
        ...
        horizentalChildView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                    //send to fatherview when stop move or just press 
                    mBinding.scrollView.requestDisallowInterceptTouchEvent(false);
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    //record positon when the finger moveing on screen 
                    moveEndX = event.getX();
                    moveEndY = event.getY();
                    //if the x row`s difference bigger than y row`s,means this move is horizental. When the difference is bigger than 150, its a effective move.
                    if ((moveEndY-startY>150||moveEndY-startY<-150)&&(moveEndX-startX<150&&moveEndX-startX>-150)){
                        mBinding.scrollView.requestDisallowInterceptTouchEvent(false);
                    }else {
                        mBinding.scrollView.requestDisallowInterceptTouchEvent(true);
                    }
                } else if (event.getAction() == MotionEvent.ACTION_BUTTON_PRESS) {
                    startX = event.getX();
                    startY = event.getY();
                }
                return false;
            }
        });
        ...
    }

在K线页面中,注意排除点击和长按事件,避免影响功能