Skip to content

Instantly share code, notes, and snippets.

@vishna
Created November 23, 2018 13:05
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 vishna/5ff3e51fe2d5ad7a957a3636ba64170c to your computer and use it in GitHub Desktop.
Save vishna/5ff3e51fe2d5ad7a957a3636ba64170c to your computer and use it in GitHub Desktop.
View.doOnNextLayout
// https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/core/ktx/src/main/java/androidx/core/view/View.kt
inline fun <T : View> T.doOnNextLayout(crossinline action: (view: T) -> Unit) {
addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
override fun onLayoutChange(
view: View,
left: Int,
top: Int,
right: Int,
bottom: Int,
oldLeft: Int,
oldTop: Int,
oldRight: Int,
oldBottom: Int
) {
view.removeOnLayoutChangeListener(this)
@Suppress("UNCHECKED_CAST")
action(view as T)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment