Skip to content

Instantly share code, notes, and snippets.

@pranavlathigara
Forked from quiro91/BindExt.kt
Created February 18, 2019 07:09
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 pranavlathigara/eca341ccfc250e4b4dca16b91151d9da to your computer and use it in GitHub Desktop.
Save pranavlathigara/eca341ccfc250e4b4dca16b91151d9da 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