Igor
Igor

Reputation: 1317

Can modify data which is being send to listener?

Can I modify data which is sent to OnTouchListener? My situation looks like this:

class A extends View{
    public A(Context context){
        super(context);
    }
    ...
}

And in some activity I have:

A a = new A(this);
a.setOntouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // event should be modified
        ....
    }
}

If a touch event occurs, I want to modify it, inside class A (for example change event's coordinates) so that inside onTouch method I can use modified event.

Upvotes: 2

Views: 30

Answers (1)

Zach
Zach

Reputation: 1964

There is no way to modify what gets sent to the onTouch, I would go this route instead. Note that Coordinates is just a made up name, I am sure something similar exists but you could just create this class if not. Any parameters that can be accessed from within the touch listener could be sent to the method as well, if not you may have to set a class variable to get the value you need to check inside of the getModifiedCoordinates method

A a = new A(this);
a.setOntouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Coordinates coords = getModifiedCoordinates(event);
        //do something with the coordinates
    }
}

...

private Coordinates getModifiedCoordinates(MotionEvent event) {
    boolean shouldBeModified = <the conditions you are checking for>;
    if(shouldBeModified)
         return new Coordinates(modified_x,modified_y);
    else
         return new Coordinates(event.getX(), event.getY());
}

Upvotes: 1

Related Questions