Android Jetpack之通用Adapter(Databinding+BRVAH)

个人原创,转载请注明出处:https://www.jianshu.com/p/bfff03765a27

之前发过一个databinding版的通用adapter,能实现一般需求,不过功能比较简陋,实际开发中大家更倾向于使用BRVAH等功能丰富的第三方框架。现在给出一个基于BRVAH的databinding版通用Adapter。

BaseAdapter

abstract class BaseAdapter
(private val brId: Int, layoutId: Int, dataList: MutableList?)
 : BaseQuickAdapter(layoutId, dataList)  {
    ...
}

构造函数比较长,两个泛型,T代表item的model类型即dataList的bean类型,TBinding代表itemLayout对应生成的XXXBinding类,比如itemLayout是fruit_item.xml,对应的就是FruitItemBinding。三个个参数dataList和layoutId应该不用解释,brId则对应itemLayout里相应variable的变量名,比如variable的name是fruit,brId就是BR.fruit

数据绑定

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        DataBindingUtil.bind(viewHolder.itemView)
    }

    final override fun convert(helper: BaseViewHolder, item: T) = convert(helper, item, helper.layoutPosition)

    //final override fun convert(helper: BaseViewHolder, item: T, payloads: List) = convert(helper, item, helper.layoutPosition, payloads)

    protected open fun convert(holder: BaseViewHolder, item: T, position: Int) {
        holder.getBinding()?.apply {
            setVariable(brId, item)
            executePendingBindings()
        }
    }

    //protected open fun convert(holder: BaseViewHolder, item: T, position: Int, payloads: List) {}

重写onItemViewHolderCreated方法传入泛型TBinding,并在convert方法里执行绑定。这里暴露了一个重载方法convert(holder: BaseViewHolder, item: T, position: Int)。用过BRVAH的程序猿都知道默认的convert方法里并不提供当前item对应的position,这点不方便导致该框架被问的最多的问题之一就是“如何在convert方法里获取position” -_-! 这里直接在原来的convert方法里调用新增的重载方法并传入当前的position,这样在子类里我们重写convert方法时就可以直接获得当前的position了。同时原convert方法被设置为final类型以确保其在子类中不被重写。

点击监听扩展

BRVAH默认的setOnItemClickListener有一点不好,就是不提供当前的item,只能通过dataList[position]adpter.getItem(position) as T来获取。前者可能需要我们将dataList设置为全局变量,后者更是需要做强制类型转换,都不是很方便。现在我们丢掉自带监听,重新实现一个能提供当前item的点击监听方法。

    private var onItemClickListener: ((BaseAdapter, View, Int, T) -> Unit)? = null

    private var onItemLongClickListener: ((BaseAdapter, View, Int, T) -> Boolean)? = null

    private var onItemChildClickListener: ((BaseAdapter, View, Int, T) -> Unit)? = null

    private var onItemChildLongClickListener: ((BaseAdapter, View, Int, T) -> Boolean)? = null

    protected open fun setOnItemClick(view: View, position: Int, item: T) {
        onItemClickListener?.invoke(this, view, position, item)
    }

    protected open fun setOnItemLongClick(view: View, position: Int, item: T): Boolean {
        return onItemLongClickListener?.invoke(this, view, position, item) ?: false
    }

    protected open fun setOnItemChildClick(view: View, position: Int, item: T) {
        onItemChildClickListener?.invoke(this, view, position, item)
    }

    protected open fun setOnItemChildLongClick(view: View, position: Int, item: T): Boolean {
        return onItemChildLongClickListener?.invoke(this, view, position, item) ?: false
    }

    fun setOnItemClickListener(listener: (BaseAdapter, View, Int, T) -> Unit) {
        onItemClickListener = listener
    }

    fun setOnItemLongClickListener(listener: (BaseAdapter, View, Int, T) -> Boolean) {
        onItemLongClickListener = listener
    }

    fun setOnItemChildClickListener(listener: (BaseAdapter, View, Int, T) -> Unit) {
        onItemChildClickListener = listener
    }

    fun setOnItemChildLongClickListener(listener: (BaseAdapter, View, Int, T) -> Boolean) {
        onItemChildLongClickListener = listener
    }

    override fun bindViewClickListener(viewHolder: BaseViewHolder, viewType: Int) {
        onItemClickListener?.let {
            viewHolder.itemView.setOnClickListener { v ->
                var position = viewHolder.adapterPosition
                if (position == RecyclerView.NO_POSITION) {
                    return@setOnClickListener
                }
                position -= headerLayoutCount
                setOnItemClick(v, position, getItem(position))
            }
        }
        onItemLongClickListener?.let {
            viewHolder.itemView.setOnLongClickListener { v ->
                var position = viewHolder.adapterPosition
                if (position == RecyclerView.NO_POSITION) {
                    return@setOnLongClickListener false
                }
                position -= headerLayoutCount
                setOnItemLongClick(v, position, getItem(position))
            }
        }

        onItemChildClickListener?.let {
            for (id in getChildClickViewIds()) {
                viewHolder.itemView.findViewById(id)?.let { childView ->
                    if (!childView.isClickable) {
                        childView.isClickable = true
                    }
                    childView.setOnClickListener { v ->
                        var position = viewHolder.adapterPosition
                        if (position == RecyclerView.NO_POSITION) {
                            return@setOnClickListener
                        }
                        position -= headerLayoutCount
                        setOnItemChildClick(v, position, getItem(position))
                    }
                }
            }
        }
        onItemChildLongClickListener?.let {
            for (id in getChildLongClickViewIds()) {
                viewHolder.itemView.findViewById(id)?.let { childView ->
                    if (!childView.isLongClickable) {
                        childView.isLongClickable = true
                    }
                    childView.setOnLongClickListener { v ->
                        var position = viewHolder.adapterPosition
                        if (position == RecyclerView.NO_POSITION) {
                            return@setOnLongClickListener false
                        }
                        position -= headerLayoutCount
                        setOnItemChildLongClick(v, position, getItem(position))
                    }
                }
            }
        }
    }

因为一并重写了长按和子控件的监听所以代码略有点长,其实只需要看onItemClickListenersetOnItemClicksetOnItemClickListenerbindViewClickListener这四个方法就行了。
首先private var onItemClickListener: ((BaseAdapter, View, Int, T) -> Unit)? = null这里onItemClickListener是一个函数对象(在kotlin里万物皆可定义为对象),具体实现在setOnItemClickListener时指定。setOnItemClick相比框架的原版方法多了一个参数item: T,以便暴露当前的item。然后重写BRVAH的bindViewClickListener方法,将原版的setOnItemLongClick方法换成我们修改版的并传入当前item。
这里顺便提一下为什么不用接口来实现监听。在java里我们通常使用接口来实现监听回调,但在kotlin里更推荐将监听接口直接定义为函数对象,因为kotlin接口不支持转换为lambda表达式,而函数对象可以(kotlin调用java接口也可以)。如果要通过接口实现同时又不舍得放弃lambda,则只能将接口写在外面的java类中。(不想多此一举的话,还是选择函数对象吧)

实现类adapter

class FruitAdapter (layoutId: Int, brId: Int, itemList: MutableList) : BaseAdapter(layoutId, brId, itemList)

一行代码就搞定了!如果需要执行其他操作,就跟用BRVAH一样重写convert方法

    override fun convert(holder: BaseViewHolder, item: Fruit, position: Int) {
        super.convert(holder, item, position) //别忘了调用父类方法,否则不会执行绑定
        ...
    }

同时BaseQuickAdapter的其他功能都被保留!

扩展阅读

BRVAH官方使用指南
Kotlin interface不能使用Lambda表达式

你可能感兴趣的:(Android Jetpack之通用Adapter(Databinding+BRVAH))