个人原创,转载请注明出处:https://www.jianshu.com/p/14ab0ebea1a4
RecyclerView写多了,就对重复的adapter感到厌烦,尤其是用上databinding后重复代码相当多,于是干脆抽取出来写个供其他adapter继承的通用的BaseAdapter。废话不多说直接上代码:
BaseAdapter
...
//两个泛型,T代表item的model类型即itemList的bean类型,TBinding代表itemLayout对应生成的XXXBinding类,比如itemLayout是fruit_item.xml,对应的就是FruitItemBinding
//两个参数itemList对应具体要显示的arrayList,brId则对应itemLayout里相应variable的变量名,比如variable的name是fruit,brId就是BR.fruit
abstract class BaseAdapter<T, TBinding: ViewDataBinding>(private val itemList: List<T>,
private val brId: Int) : RecyclerView.Adapter<BaseAdapter<T, TBinding>.ViewHolder>() {
protected var mContext: Context? = null
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
lateinit var binding: TBinding
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
if (mContext == null) {
mContext = parent.context
}
//因为TBinding的具体类型不确定,这里只能用DataBindingUtil.inflate(),而不能用XXXBinding.inflate()
val binding: TBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), getLayoutId(), parent, false)
val holder = ViewHolder(binding.root)
holder.binding = binding
return holder
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.binding.setVariable(brId, itemList[position])
//立即执行绑定,在对view变化时效敏感的地方常用,不加这句有可能出现itemView更新滞后、闪烁等问题
holder.binding.executePendingBindings()
}
override fun getItemCount(): Int {
return itemList.size
}
//获取具体子adapter对应的itemLayoutId
abstract fun getLayoutId(): Int
}
代码注释说的很详细了,除了基本的显示功能外,只重写了一个常用的getItemCount()
。下面我们来看看具体的子类adapter:
FruitAdapter
package com.zerlings.gabeisfaker.ui.main
...
class FruitAdapter (itemList: List<Fruit>, brId: Int) : BaseAdapter<Fruit, FruitItemBinding>(itemList, brId) {
override fun getLayoutId(): Int {
return R.layout.fruit_item //item对应的layoutId
}
}
是不是超级简单!省掉了不少重复的代码!当然这只包含了最基本的代码,通常我们还需要做一些扩展功能,比如点击事件:
var onItemClickListener: OnItemClickListener? = null
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
super.onBindViewHolder(holder, position)
holder.itemView.setOnClickListener { v -> onItemClickListener?.onItemClick(v, position) }
}
interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
}
以及添加表头:
companion object {
private const val TYPE_HEADER = 1
private const val TYPE_NORMAL = 0
}
var headerView: View? = null
set(headerView) {
field = headerView
notifyItemInserted(0)
}
override fun getItemViewType(position: Int): Int {
return if (position == 0 && headerView != null) {
//第一个item应该加载Header
TYPE_HEADER
} else {
TYPE_NORMAL
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return if (viewType == TYPE_HEADER) ViewHolder(headerView!!) else super.onCreateViewHolder(parent, viewType)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (getItemViewType(position) == TYPE_NORMAL) super.onBindViewHolder(holder, position)
}
最后完整的FruitAdapter:
...
class FruitAdapter (itemList: List<Fruit>, brId: Int) : BaseAdapter<Fruit, FruitItemBinding>(itemList, brId) {
companion object {
private const val TYPE_HEADER = 1
private const val TYPE_NORMAL = 0
}
var onItemClickListener: OnItemClickListener? = null
var headerView: View? = null
set(headerView) {
field = headerView
notifyItemInserted(0)
}
override fun getItemViewType(position: Int): Int {
return if (position == 0 && headerView != null) {
//第一个item应该加载Header
TYPE_HEADER
} else {
TYPE_NORMAL
}
}
override fun getLayoutId(): Int {
return R.layout.fruit_item //item对应的layoutId
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return if (viewType == TYPE_HEADER) ViewHolder(headerView!!) else super.onCreateViewHolder(parent, viewType)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (getItemViewType(position) == TYPE_NORMAL) {
super.onBindViewHolder(holder, position)
holder.itemView.setOnClickListener { v -> onItemClickListener?.onItemClick(v, position) }
}
}
interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
}
}
这个FruitAdapter只扩展了添加header和点击监听两个常用功能,有更多需求可继续扩展,这里就不再深入了。
至于具体的Fruit.class类和fruit_item.xml是什么样,已经不重要了。
网友评论