This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 149
Add android view binding support #114
Comments
+1 on this |
DatabindingBinder: package com.fyeo.util.mva3
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import mva3.adapter.ItemBinder
abstract class DataBindingItemBinder<M, VDB : ViewDataBinding>(@LayoutRes private val layoutRes: Int) :
ItemBinder<M, DataBindingItemBinder.ViewHolder<M, VDB>>() {
override fun createViewHolder(parent: ViewGroup): ViewHolder<M, VDB> {
return createViewHolder(createBinding(parent))
}
override fun bindViewHolder(
holder: ViewHolder<M, VDB>,
item: M
) {
bindModel(item, holder)
holder.binding.executePendingBindings()
}
override fun bindViewHolder(
holder: ViewHolder<M, VDB>,
item: M,
payloads: MutableList<Any?>?
) {
bindModel(item, holder, payloads)
holder.binding.executePendingBindings()
}
private fun createViewHolder(binding: VDB): ViewHolder<M, VDB> {
return ViewHolder(binding)
}
protected abstract fun bindModel(item: M, holder: ViewHolder<M, VDB>)
protected open fun bindModel(
item: M,
holder: ViewHolder<M, VDB>,
payloads: MutableList<Any?>?
) {
bindModel(item, holder)
}
private fun createBinding(parent: ViewGroup): VDB {
val inflater = LayoutInflater.from(parent.context)
return DataBindingUtil.inflate(inflater, layoutRes, parent, false)
}
class ViewHolder<M, VDB : ViewDataBinding?>(binding: VDB) :
BindingViewHolder<M, VDB>(binding)
} Databinding with DiffUtils support: package com.fyeo.util.mva3
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import mva3.adapter.ItemBinder
abstract class DBItemBinderWithPayload<M, VDB : ViewDataBinding?> :
ItemBinder<M, DBItemBinderWithPayload.ViewHolder<M?, VDB?>>() {
override fun createViewHolder(parent: ViewGroup): ViewHolder<M?, VDB?> {
return createViewHolder(createBinding(parent))
}
override fun bindViewHolder(
holder: ViewHolder<M?, VDB?>,
item: M
) {
bindModel(item, holder.binding!!)
holder.binding.executePendingBindings()
}
override fun bindViewHolder(
holder: ViewHolder<M?, VDB?>,
item: M,
payloads: MutableList<Any?>?
) {
bindModel(item, holder.binding!!, payloads)
holder.binding.executePendingBindings()
}
private fun createViewHolder(binding: VDB): ViewHolder<M?, VDB?> {
return ViewHolder(binding)
}
protected abstract fun bindModel(item: M, binding: VDB)
protected open fun bindModel(item: M, binding: VDB, payloads: MutableList<Any?>?) {
bindModel(item, binding)
}
protected abstract fun createBinding(parent: ViewGroup): VDB
class ViewHolder<M, VDB : ViewDataBinding?>(binding: VDB) :
BindingViewHolder<M, VDB>(binding)
}
BindingViewHolder.kt abstract class BindingViewHolder<M, VDB : ViewDataBinding?>(val binding: VDB) :
ItemViewHolder<M>(binding!!.root) |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add android view binding support
The text was updated successfully, but these errors were encountered: