Home > OS >  Passing an inner class to gestureDetectorcompat as parameter
Passing an inner class to gestureDetectorcompat as parameter

Time:12-16

Im trying to handle swipes on an app im building. Found a way of doing this on youtube but it uses kotlin not java.

The method works with an inner class

class homepageGestureListener extends GestureDetector.SimpleOnGestureListener

and in the super class i have

private GestureDetectorCompat detector;//needed to wire inner class to main activity

Below that, in the onCreate function im trying to assign the gestureDetector to the "detector"

detector=GestureDetectorCompat(this,homepageGestureListener());

this line gives the error "method call expected", it wont take homepageGestureListener() as a paramater.

How can i pass an inner class there?

CodePudding user response:

GestureDetectorCompat has 2 public constructors one that take a context and a listener and the other taking a context, a listener and a handler. In your case it's the first one. You need to pass an instance of your listener as the 2nd argument. Someting like this:

//...

private GestureDetectorCompat mGestureDetector;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main_activity);
        
        //...

        mGestureDetector = new GestureDetectorCompat(this, new LongPressListener()); // here passing LongPressListener()
    }

//..
}

Check on this link on the official documentation to learn more.

EDIT: Oh, your class extends GestureDetector.SimpleOnGestureListener. Then in this case juste pass this as both arguments.

Like:

private GestureDetectorCompat detector

Then:

detector = new GestureDetectorCompat(this, new homepageGestureListener()); 

I slightly edited the answer so you can pass an instance of your inner class instead.

  • Related