自定义视图上的 Ontouch 侦听器

Ontouch Listener on Custom View

我有一个自定义视图,可以在屏幕上拖动,点击它会显示另一个视图,在模拟器中使用时一切正常。但是在真实设备上,自定义视图不会在点击时显示新视图。我很难理解为什么。

我认为这就是问题所在: 当我将视图放在屏幕的最左侧时,另一个视图只会在点击时显示,但是如果我现在将符号 > 更改为 <视图可以从最右侧打开,但不能从左侧打开。

 case MotionEvent.ACTION_MOVE:
                    if(motionEvent.getRawX() > 120 && motionEvent.getRawY() > 120){
                        params.x = initialX + (int) (motionEvent.getRawX() - initialTouchX);
                        params.y = initialY + (int) (motionEvent.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(entireLayout, params);

                        moving = true;
                    }

这是完整的代码:

  collapsedLayout.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = motionEvent.getRawX();
                    initialTouchY = motionEvent.getRawY();

                    return true;

                case MotionEvent.ACTION_MOVE:
                    if(motionEvent.getRawX() > 120 && motionEvent.getRawY() > 120){
                        params.x = initialX + (int) (motionEvent.getRawX() - initialTouchX);
                        params.y = initialY + (int) (motionEvent.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(entireLayout, params);

                        moving = true;
                    }
                    return true;

                case MotionEvent.ACTION_UP:

                    if(moving){
                        if (params.x > 0) {
                            params.x = (int) dpWidth;
                        } else if (params.x < 0) {
                            params.x = (int) dpWidth * -1;
                        }
                        mWindowManager.updateViewLayout(entireLayout, params);
                        moving = false;
                    }else {
                        collapsedLayout.setVisibility(View.GONE);
                        expandedLayout.setVisibility(View.VISIBLE);
                    }
                    return true;
            }
            return false;
        }
    });

在此先感谢您的帮助!

感谢 RobVoisey,我找到了如何做的方法。我为视图的拖动实现了一个 Ontouch 侦听器,并在点击时实现了一个用于显示视图的单击侦听器。我使用了这个解决方案 how to use both Ontouch and Onclick for an ImageButton?

对于想要看到答案的人来说,实现方式是这样的:

   private GestureDetector gestureDetector;

   public void displayOverlay() {
   gestureDetector = new GestureDetector(this, new SingleTapConfirm());

   @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (gestureDetector.onTouchEvent(event)) {

                return true;
            } else {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;

                    case MotionEvent.ACTION_UP:
                        if (params.x > 0) {
                            params.x = (int) dpWidth;
                        } else if (params.x < 0) {
                            params.x = (int) dpWidth * -1;
                        }
                        mWindowManager.updateViewLayout(entireLayout, params);
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(entireLayout, params);
                        return true;
                }
            }
            return false;
        }
    });
   }


 private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        collapsedLayout.setVisibility(View.GONE);
        expandedLayout.setVisibility(View.VISIBLE);

        return true;
    }
}