Skip to content

Instantly share code, notes, and snippets.

@twocity
Created June 28, 2017 03:35
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 twocity/b851144601b2bcb531512013675a8702 to your computer and use it in GitHub Desktop.
Save twocity/b851144601b2bcb531512013675a8702 to your computer and use it in GitHub Desktop.
EpoxyHolder' views init
abstract class BaseEpoxyHolder : EpoxyHolder() {
lateinit var itemView: View
final override fun bindView(view: View?) {
this.itemView = view!!
onBindView(itemView)
}
abstract fun onBindView(view: View)
}
fun <V : View> BaseEpoxyHolder.viewId(id: Int)
: ReadOnlyProperty<BaseEpoxyHolder, V> = Lazy { _, _ ->
this.itemView.findViewById(id) as V
}
private class Lazy<T, R>(private val initializer: (T, KProperty<*>) -> R) : ReadOnlyProperty<T, R> {
private object EMPTY
private var value: Any? = EMPTY
override fun getValue(thisRef: T, property: KProperty<*>): R {
if (value == EMPTY) {
value = initializer(thisRef, property)
}
return value as R
}
}
class ItemModelHolder : BaseEpoxyHolder() {
val draweeView: SimpleDraweeView by viewId(R.id.image_home_recommend_product)
val titleView: TextView by viewId(R.id.text_home_recommend_product_title)
override fun onBindView(view: View) {
draweeView.aspectRatio = 16.0f / 9.0f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment