Skip to content

Instantly share code, notes, and snippets.

@afollestad
Created November 30, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afollestad/62fd4afa86883b7aba83ee0589d09ebc to your computer and use it in GitHub Desktop.
Save afollestad/62fd4afa86883b7aba83ee0589d09ebc to your computer and use it in GitHub Desktop.
import android.view.View
import android.view.ViewTreeObserver
fun View.onLayout(cb: () -> Unit) {
if (this.viewTreeObserver.isAlive) {
this.viewTreeObserver.addOnGlobalLayoutListener(
object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
cb()
this@onLayout.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment