How To apply click listener?

懵懂的女人 提交于 2019-12-05 16:07:18

I ran into a similar problem and. To get it working I have a parent layout (contentContainer) which contains 2 subviews:

  • Content view which is converted to bitmap when curling
  • CurlView (OpenGL Surface)

I had to set an onClickListener to the parent layout to catch the events and send or not send them to the CurlView. When the events are sent to the CurlView, the CurlView is brought to front (using bringToFront() method). Otherwise, the content view is brought to front.

To decide if events are sent or not to the CurlView I use a method that returns true if the tap was within the page curl area (aprox. 1/6 of the screen, both sides). Additionally I have a gesture and scale detectors to detect singleTap and longPress, which is what we need for user interaction. When a singleTap or longPress is catched, the postion (x,y) tells me where the user touched so I can launch some activity, show a context menu or whatever.

Some sample code:

    contentContainer.setOnClickListener(null); // DO NOT REMOVE THIS, IT'S A WORKAROUND
    contentContainer.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            mScaleDetector.onTouchEvent(event);

            int x = (int)event.getX();
            int y = (int)event.getY();

            boolean isMoving = event.getAction() == MotionEvent.ACTION_MOVE;
            boolean isTouchDown = event.getAction() == MotionEvent.ACTION_DOWN;
            boolean isTouchUp = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
            boolean pageCurlArea = isPageCurlArea(x, y);

            boolean sendEventToCurlView = false;

            if (isTouchDown && pageCurlArea) {
                downInPageArea = true;
                sendEventToCurlView = true;
            } else if (downInPageArea && isMoving) {
                sendEventToCurlView = true;
            } else if (downInPageArea && isTouchUp) {
                downInPageArea = false;
                sendEventToCurlView = true;
            } else if (!downInPageArea && isMoving) {
                return false;
            }

            if (downInPageArea && isMoving && !curlViewIsOnTop) {
                bringCurlViewToFront();
            }

            if (sendEventToCurlView) {
                MotionEvent mEvent = MotionEvent.obtain(event);
                mCurlView.onTouch(v, mEvent);
            }

            return !sendEventToCurlView;
        }
    });

It is a work in progress yet, because I need to modify it to not use "page curl area" to discriminate between "curl event" and other events (singleTap, longPress...), for which I plan to use both the gesture and scale detectors to return if an event was catched and depending on the result, continue sending the event to the CurlView or not. This is necessary to perform "taps" and "longPress" within the 1/6 page curl area.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!