1、直接上Adapter的代码
class MyAdapter : ListAdapter<UserInfo, MyAdapter.MyViewHolder>(MyCallback()) {
private var listener: ((position: Int) -> Unit)? = null
var mList: MutableList<UserInfo>? = null
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_my, parent, false)
)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val userInfo = currentList[position]
val itemView = holder.itemView
println("更新了条目$position $userInfo")
userInfo.apply {
itemView.tvUserId.text = userId.toString()
itemView.tvToken.text = token
}
itemView.setBackgroundColor(
ContextCompat.getColor(
itemView.context,
if (userInfo.isSelect) {
R.color.purple_200
} else {
R.color.white
}
)
)
/* itemView.setOnClickListener {
listener?.invoke(position)
}*/
holder.setDiffItemClickListener {
listener?.invoke(it)
}
}
override fun getItemCount(): Int = currentList.size
/* override fun getItemId(position: Int): Long {
return position.toLong()
}*/
fun getList() = currentList
fun setData(list: MutableList<UserInfo>?) {
mList = list
submitList(list)
}
fun setItemClickListener(listener: (position: Int) -> Unit) {
this.listener = listener
}
class MyCallback : DiffUtil.ItemCallback<UserInfo>() {
override fun areItemsTheSame(
oldItem: UserInfo,
newItem: UserInfo
): Boolean {
return oldItem.userId == newItem.userId
}
override fun areContentsTheSame(
oldItem: UserInfo,
newItem: UserInfo
): Boolean {
return oldItem == newItem
}
}
}
需要注意点①需要继承ListAdapter ②通过currentList获取list数据 ③ 定义一个MyCallBack,里面的areItemsTheSame方法 可以返回唯一的id比较,areContentsTheSame 可以返回对象重写equals后的比较内容是否相同 ④在设置数据的时候调用submitList方法,该方法需要每次调用传入一个新的list,否则不跟新数据 ⑤最大的坑条目的点击事件获取索引不要直接使用onBindViewHolder参数里的position,可能会发生索引不准确问题,正确的获取索引的方式如下:
fun RecyclerView.ViewHolder.setDiffItemClickListener(function: (position: Int) -> Unit) {
this.itemView.tag = this
this.itemView.setOnClickListener {
if (it.tag is RecyclerView.ViewHolder) {
val adapterPosition = (it.tag as RecyclerView.ViewHolder).adapterPosition
function.invoke(adapterPosition)
}
}
}
这样就不会出现点击条目获取的索引不准确问题