3

I have a Custom Layout with a standard setOnClickListener() call.

setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // some click related code
            }
        });

In this Layout is a view that should drag horizontally.

I wrote some code and it does more or less what I want it to do:

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = true;
    final int action = event.getAction();
    final float x = event.getRawX();
    final float y = event.getRawY();
    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            mInitialTouchX = x;
            mInitialTouchY = y;

            mStartX = getX();
            mStartY = getY();
            break;
        }
        case MotionEvent.ACTION_MOVE: {
            Logger.d("MOVE");
            if (direction == -1) {

            }

            final float dx = x - mInitialTouchX;
            final float dy = y - mInitialTouchY;

            mPosX = mStartX + dx;
            mPosY = mStartY + dy;

            if (Math.abs(dx) > SWYPE_MIN_TRAVEL) {
                if (Math.abs(mPosX) < maximumSwypeDistance) {
                    setTranslationX(mPosX);
                }
            }

            break;
        }
        case MotionEvent.ACTION_UP:
            Logger.d("UP");
        case MotionEvent.ACTION_CANCEL:
            Logger.d("CANCEL");
        case MotionEvent.ACTION_POINTER_UP:
            Logger.d("POINTER_UP");
            final float dx = x - mInitialTouchX;
            if (Math.abs(dx) < SWYPE_MIN_TRAVEL) {
                Logger.d("click");
                result = false;
            }
            break;
    }
    return result;
}

The question is: How can I have both?

Edit: Well using the answer from Gunnar Karlssons event i could alter the code to perform the click manually in the ACTION_UP Event like this:

((ViewGroup) this.getParent()).performClick();

I can't help myself but i find this somewhat unsatisfying. I there a more elegant solution?

Philipp Redeker
  • 3,848
  • 4
  • 26
  • 34

1 Answers1

0

The short answer is yes, you can achieve this behavior. The long answer is that you need to make sure that both views get the touch events as you expect them. Depending on the exact layouts and the expected behavior you want, you can use a couple of methods:

  1. You don't have to return true from onTouchEvent() - that way the touch event would go to the parent as well (just make sure that you are getting the next event).
  2. Use onInterceptTouchEvent() in the parent and detect the click by using a GestureDetector and SimpleOnGestureListener.

    GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener(){
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // some click related code
            return true;
        }
    };
    
    GestureDetector gestureDetector = new GestureDetector(context, simpleOnGestureListener);
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent e){
        gestureDetector.onTouchEvent(e);
        return false;
    }
    
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60