How to reference anonymous inner class in Kotlin? -


i have following extension method. how can reference ongloballayoutlistener passed addongloballayoutlistener() method? need pass listener removeongloballayoutlistener() method.

fun view.ongloballayout(callback:() -> unit ): unit{     this.viewtreeobserver.addongloballayoutlistener {         if(android.os.build.version.sdk_int >= android.os.build.version_codes.jelly_bean) {             this.viewtreeobserver.removeongloballayoutlistener(this);         }         else {             this.viewtreeobserver.removeglobalonlayoutlistener(this);         }         callback();     } } 

one way use object expression instead of lambda so:

fun view.ongloballayout(callback: () -> unit): unit {     val viewtreeobserver = this.viewtreeobserver     viewtreeobserver.addongloballayoutlistener(object : viewtreeobserver.ongloballayoutlistener {         override fun ongloballayout() {             if (android.os.build.version.sdk_int >= android.os.build.version_codes.jelly_bean) {                 viewtreeobserver.removeongloballayoutlistener(this);             } else {                 viewtreeobserver.removeglobalonlayoutlistener(this);             }             callback();         }     }) } 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -