electronix384128
electronix384128

Reputation: 6723

How to Hook into Existing Event Listeners

I am currently working on a Java SDK which gets installed by the original developer in his existing Android app. Now I need to set some event listeners e.g.:

public void registerTouchListener(ScrollView scrollview) {
  scrollview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      Log.d("SDK", event.toString());
      return false;
    }
  });
}

The issue is that the passed in scrollview might already have a registered listener, so my question is how I can hook into the existing listener or register a second listener?

Upvotes: 3

Views: 273

Answers (2)

pfrank
pfrank

Reputation: 2167

Maybe too hacky, but could you somehow "Steal" the touch event before it gets to the Scrollview by checking the coordinates of where the touch is going to land and if it's in your list of registered Scrollview. Then act as if you haven't handled the touch event -- that way you could act on that Scrollview prior to it being passed on the touch event?

Upvotes: 1

pfrank
pfrank

Reputation: 2167

Rather than providing the API like this, could you rather provide an API that had a function which would call onTouch directly at the end (or beginning) of their event? Not quite as friendly because it won't live in the initialization section, but it will be much clearer and less buggy.

Upvotes: 1

Related Questions