Skip to content

Instantly share code, notes, and snippets.

@mr5z
Last active August 11, 2020 14:11
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 mr5z/94314d6bf8015b66381eea8cc6f53ec9 to your computer and use it in GitHub Desktop.
Save mr5z/94314d6bf8015b66381eea8cc6f53ec9 to your computer and use it in GitHub Desktop.
private class LayoutObserver(private val view: View, private val callback: (Size) -> Unit) : ViewTreeObserver.OnGlobalLayoutListener {
init {
if (!view.viewTreeObserver.isAlive)
throw IllegalArgumentException("Go fuck yourself")
view.viewTreeObserver.addOnGlobalLayoutListener(this)
}
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
callback(Size(view.width, view.height))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment