Coder Social home page Coder Social logo

Comments (2)

MDXDave avatar MDXDave commented on June 26, 2024 1

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)

from multiviewadapter.

AlonShahaf avatar AlonShahaf commented on June 26, 2024

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

from multiviewadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.