Skip to content

Instantly share code, notes, and snippets.

@bholota
Forked from quiro91/BindExt.kt
Created April 23, 2017 16:43
Show Gist options
  • Save bholota/b50390988323b37d0d29b854f76332c0 to your computer and use it in GitHub Desktop.
Save bholota/b50390988323b37d0d29b854f76332c0 to your computer and use it in GitHub Desktop.
import android.app.Activity
import android.support.annotation.IdRes
import android.view.View
fun <T : View> Activity.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById(idRes) as T }
}
fun <T : View> View.bind(@IdRes idRes: Int): Lazy<T> {
@Suppress("UNCHECKED_CAST")
return unsafeLazy { findViewById(idRes) as T }
}
private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE, initializer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment