androidandroid-viewandroid-event

How to allow several listeners to handle the same OnTouch events in Android?


I have two different objects that implement OnTouchListener.

One of them, uses the event data to modify some internal data, and the other one Updates the UI.

The problem is that if I set them both to listen to the same View, only one of them handles the OnTouch event, the other one's OnTouch function is not called.

I read that if the OnTouch function returns true, that means that the event has been handled, but if I return False, they dont behave properly.

So, the question is:

How can I set two or more listener objects to the same OnTouch events from a View, and allow them all to receive all those events?


Solution

  • Just have one listener call a method for both Objects:

    Object uiModifier;
    Object internalDataModifier;
    //ensure these Objects are initialized
    
    myView.setOnClickListener(new OnTouchListener() {
        @Override
        public boolean onTouchListener(View v, MotionEvent event)
        {
            internalDataModifier.handleTouch(v, event);
            uiModifier.handleTouch(v, event);
            return true;
        }
    
    }
    

    and ensure that both internalDataModifier and uiModifier have a handleTouch(View, MotionEvent) method.