Skip to content

Instantly share code, notes, and snippets.

@liufsd
Forked from quiro91/BindExt.kt
Created July 27, 2017 03:28
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 liufsd/3f841ccc543e1a8027630f3f0eecfd42 to your computer and use it in GitHub Desktop.
Save liufsd/3f841ccc543e1a8027630f3f0eecfd42 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