美文网首页
Android ViewDataBinding 与 Recycl

Android ViewDataBinding 与 Recycl

作者: 靳兆鲁 | 来源:发表于2021-04-27 17:15 被阅读0次

转载请注明出处!!!!

要想让ViewDataBinding与RecyclerView完美结合,我们首先需要封装RecyclerView.ViewHolder与RecyclerView.Adapter。

一、封装RecyclerView.ViewHolder

class BindingViewHolder<T : ViewBinding>(val viewBinding: T) : RecyclerView.ViewHolder(viewBinding.root)

二、封装RecyclerView. Adapter

1. BindingRecyclerViewAdapter

abstract class BindingRecyclerViewAdapter<VB : ViewBinding> : RecyclerView.Adapter<BindingViewHolder<VB>>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingViewHolder<VB> {
        val viewBinding = onCreateViewBinding(LayoutInflater.from(parent.context), parent, viewType, false)
        return BindingViewHolder(viewBinding)
    }

    abstract fun onCreateViewBinding(inflater: LayoutInflater, root: ViewGroup?, viewType: Int, attachToRoot: Boolean): VB

    @CallSuper
    override fun onBindViewHolder(holder: BindingViewHolder<VB>, position: Int) {
        if (holder.viewBinding is ViewDataBinding) {
            holder.viewBinding.executePendingBindings()
        }
    }
}

BindingRecyclerViewAdapter 实现了对RecyclerView.Adapter 基本封装

2. ObservableBindingRecyclerViewAdapter

abstract class ObservableBindingRecyclerViewAdapter<E, VB : ViewBinding>(
        val dataList: ObservableList<E>
) : BindingRecyclerViewAdapter<VB>() {

    private val dataListChangedCallback: DataListChangedCallback = DataListChangedCallback()

    override fun onBindViewHolder(holder: BindingViewHolder<VB>, position: Int) {
        super.onBindViewHolder(holder, position)
        onBindViewHolderViewBinding(holder, position, holder.viewBinding,
                dataList[getDataItemPosition(position)])
    }

    open fun onBindViewHolderViewBinding(holder: BindingViewHolder<VB>, position: Int,
                                         viewBinding: VB, item: E) {

    }

    open fun getDataItemPosition(listPosition: Int): Int {
        return listPosition
    }

    override fun onBindViewHolder(holder: BindingViewHolder<VB>, position: Int, payloads: MutableList<Any>) {
        super.onBindViewHolder(holder, position, payloads)
        onBindViewHolderViewBinding(holder, position,
                holder.viewBinding, dataList[getDataItemPosition(position)], payloads)
    }

    open fun onBindViewHolderViewBinding(holder: BindingViewHolder<VB>, position: Int,
                                         viewBinding: VB, item: E, payloads: MutableList<Any>) {
    }

    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        dataList.addOnListChangedCallback(dataListChangedCallback)
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        dataList.removeOnListChangedCallback(dataListChangedCallback)
    }

    override fun getItemCount() = dataList.size

    private inner class DataListChangedCallback : ObservableList.OnListChangedCallback<ObservableList<E>>() {

        override fun onChanged(sender: ObservableList<E>?) {
            this@ObservableBindingRecyclerViewAdapter.notifyDataSetChanged()
        }

        override fun onItemRangeChanged(sender: ObservableList<E>?, positionStart: Int, itemCount: Int) {
            this@ObservableBindingRecyclerViewAdapter.notifyItemRangeChanged(positionStart, itemCount)
        }

        override fun onItemRangeInserted(sender: ObservableList<E>?, positionStart: Int, itemCount: Int) {
            this@ObservableBindingRecyclerViewAdapter.notifyItemRangeInserted(positionStart, itemCount)
        }

        override fun onItemRangeMoved(sender: ObservableList<E>?, fromPosition: Int, toPosition: Int, itemCount: Int) {
            this@ObservableBindingRecyclerViewAdapter.notifyDataSetChanged()
        }

        override fun onItemRangeRemoved(sender: ObservableList<E>?, positionStart: Int, itemCount: Int) {
            this@ObservableBindingRecyclerViewAdapter.notifyItemRangeRemoved(positionStart, itemCount)
        }

    }

}

ObservableBindingRecyclerViewAdapter对可观察数据集合的封装。注意操作集合时要在主线程。

三、使用

class SampleListAdapter(
        private val sampleItems: ObservableList<SampleItem>
) : ObservableBindingRecyclerViewAdapter<SampleItem, SampleItemBinding>(sampleItems) {

    override fun onCreateViewBinding(inflater: LayoutInflater, root: ViewGroup?, viewType: Int,
                                     attachToRoot: Boolean): SampleItemBinding {
        return SampleItemBinding.inflate(inflater, root, attachToRoot)
    }

    override fun onBindViewHolderViewBinding(holder: BindingViewHolder<SampleItemBinding>,
                                             position: Int, viewBinding: SampleItemBinding, item: SampleItem) {
        viewBinding.sampleItem = item
    }
}
viewBinding.recyclerView. ···
viewBinding.recyclerView.adapter = SampleListAdapter(sampleItems)
viewBinding.recyclerView. ···

相关文章

网友评论

      本文标题:Android ViewDataBinding 与 Recycl

      本文链接:https://www.haomeiwen.com/subject/twhprltx.html