https://kotlinlang.org logo
Title
j

Jan

05/13/2019, 11:58 AM
hi, can someone explain to me why the first class which uses anymous object: implementation doesn't work but the 2nd example does work? first example shows the log message, the 2nd one doesn't, it seems like the listener isn't inside the app after compile (disclaimer i'm using r8/d8)
🤷‍♂️ 3
d

dave

05/13/2019, 1:46 PM
Hi, did you try doing:
class MySampleView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private val gestureDetector: GestureDetector
        get() = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
            override fun onDown(e: MotionEvent?): Boolean {
                return super.onDown(e)
            }
        })

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        gestureDetector.onTouchEvent(event)
        return super.onTouchEvent(event)
    }
}
h

Hubert

05/13/2019, 2:19 PM
what about decompiling the APK? maybe there would you be able to see some differences in the output?
j

Jan

05/14/2019, 6:39 AM
good suggestion, will try, thing is for some reason the annoymous classes don't always seem to work for me when setting listener, no idea why