Skip to content
This repository has been archived by the owner on Jan 2, 2021. It is now read-only.

Add android view binding support #114

Open
araxis opened this issue Apr 3, 2020 · 2 comments
Open

Add android view binding support #114

araxis opened this issue Apr 3, 2020 · 2 comments

Comments

@araxis
Copy link

araxis commented Apr 3, 2020

Add android view binding support

@AlonShahaf
Copy link

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

@MDXDave
Copy link

MDXDave commented Sep 30, 2020

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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants