abstract class BaseEpoxyHolder : EpoxyHolder() { lateinit var view: View val context: Context get() = view.context @CallSuper override fun bindView(itemView: View) { view = itemView } protected fun bind(id: Int): ReadOnlyProperty = Lazy { holder: BaseEpoxyHolder, prop -> holder.view.findViewById(id) as V? ?: throw IllegalStateException("View ID $id for '${prop.name}' not found.") } private class Lazy(private val initializer: (BaseEpoxyHolder, KProperty<*>) -> V) : ReadOnlyProperty { private object EMPTY private var value: Any? = EMPTY /** * Taken from Kotterknife. * https://github.com/JakeWharton/kotterknife */ @Suppress("UNCHECKED_CAST") override fun getValue(thisRef: BaseEpoxyHolder, property: KProperty<*>): V { if (value == EMPTY) value = initializer(thisRef, property) return value as V } } }