Last active
November 4, 2017 00:28
-
-
Save justasm/ecb587ea45d56e254c078059bbc8ed26 to your computer and use it in GitHub Desktop.
Android View Kotlin extension for a one-shot callback after layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun View.afterLayout(block: View.() -> Unit) { | |
val originalVto = viewTreeObserver | |
originalVto.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener { | |
override fun onPreDraw(): Boolean { | |
if (originalVto.isAlive) originalVto.removeOnPreDrawListener(this) | |
viewTreeObserver.removeOnPreDrawListener(this) | |
block(this@afterLayout) | |
return true | |
} | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// usage | |
image_view.afterLayout { Log.d("demo", "image width $width height $height") } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment