DevAhamed / MultiViewAdapter

Easily create complex recyclerview adapters in android
https://devahamed.github.io/MultiViewAdapter
Apache License 2.0
818 stars 149 forks source link

Add android view binding support #114

Open araxis opened 4 years ago

araxis commented 4 years ago

Add android view binding support

AlonShahaf commented 4 years ago

+1 on this Meanwhile, you can write some BindingAdapters to do the job.

MDXDave commented 4 years ago

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)