Alex
Alex

Reputation: 1469

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?

Upvotes: 3

Views: 3740

Answers (4)

Erlang Parasu
Erlang Parasu

Reputation: 363

in Kotlin, i do this:

extend the View, then override setOnTouchListener, then call super.setOnTouchListener and pass a initialized listener object.

Snipets:

var customOnTouchListener: OnTouchListener? = OnTouchListener { v, event ->
    return@OnTouchListener false
}

override fun setOnTouchListener(l: OnTouchListener?) {
    val bridgeListener = OnTouchListener { v, event ->
        // Listener 2
        customOnTouchListener?.onTouch(v, event)

        // Listener 1
        l?.onTouch(v, event) ?: false
    }

    super.setOnTouchListener(bridgeListener)
}

then i use it like this:

customScrollView.setOnTouchListener { v, event -> 
    // Got the event
}
customScrollView.customOnTouchListener = View.OnTouchListener { v, event ->
    // Got the event too
}

Upvotes: 0

Badmash
Badmash

Reputation: 25

My workaround for this was to have only one class extend SimpleOnGestureListener and implement OnTouchListener. One class was extending SimpleOnGestureListener while the other class was implementing OnTouchListener but both were handling on touch events.

I then moved all the code for the smaller class (in terms of lines of code) to the larger class, then called the larger class in my implementation and it worked. I then declared my class as so

    public class MyOnlyListener extends SimpleOnGestureListener implements OnTouchListener

For reference sake I was using one class to pan zoom an image (the larger class) while I was using the other to swipe to the next page in a view.

Upvotes: 0

Phil
Phil

Reputation: 36289

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.

Upvotes: 4

thomas.cloud
thomas.cloud

Reputation: 963

Why can't you have the listener point to only the UI activity and then from that activity you can call whatever is updating the internal data? I don't think having 2 event listeners responding to the same event is a good idea even if it is possible(which I doubt). Imagine if someone tried to have 2 UI activities responding to 1 event and the clashes it would cause.

Upvotes: 0

Related Questions