Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
fun <T : View> View.bindView(@IdRes res: Int): MutableBindViewDelegate<T> {
return MutableBindViewDelegate(lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(res)!! })
class MutableBindViewDelegate<T>(private val lazy: Lazy<T>) {
private var value: T? = null
operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
return value ?: lazy.getValue(thisRef, property)
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
this.value = value
// Usage:
var title: TextView by bindView(
var body: TextView by bindView(
var ctaButton: Button by bindView(
var tagView: TextView by bindView(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.