Adapter Base基类
package com.niudong.lib_pays.adapter
import com.niudong.lib_pays.BaseViewHolder
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.lang.RuntimeException
/**
* TODO Adapter Base基类 支持多类型 Head+List
*/
abstract class BaseListAdapter<T> :
RecyclerView.Adapter<BaseViewHolder<T>>() {
private lateinit var mData: List<T>
companion object {
const val TYPE_HEAD = 0
const val TYPE_LIST = 1
var mLayout = HashMap<Int, Int>()
}
//头部点击 携带文案
var itemHeadClickListener: ((v: String) -> Unit)? = null
var itemListClickListener: ((v: T, pos: Int) -> Boolean)? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<T> {
if (mLayout.isNullOrEmpty()) throw RuntimeException("添加mLayout")
val get = mLayout[viewType]
val view = LayoutInflater.from(parent.context)
.inflate(get!!, parent, false)
//标记tag
view.tag = viewType
return BaseViewHolder(view, itemListClickListener, itemHeadClickListener)
}
override fun onBindViewHolder(holder: BaseViewHolder<T>, position: Int) {
val pos = if (holder.itemViewType == TYPE_HEAD || !isHasHead()) position else position - 1
bindData(holder, mData[pos])
holder.setData(mData[pos])
}
abstract fun isHasHead(): Boolean
abstract fun bindData(
holder: BaseViewHolder<T>,
data: T
)
abstract fun addTypeAndLayout()
override fun getItemCount(): Int {
return mData.size + if (isHasHead()) 1 else 0
}
fun setData(listData: List<T>) {
this.mData = listData
notifyDataSetChanged()
}
override fun getItemViewType(position: Int): Int {
return if (position == 0 && isHasHead()) {
TYPE_HEAD
} else {
TYPE_LIST
}
}
}
Adapter 子类继承
package com.niudong.lib_pays.adapter
import com.niudong.lib_pays.BaseViewHolder
import android.graphics.Color
import com.niudong.lib_pays.R
import com.niudong.lib_pays.bean.PayData
class TestAdapter : BaseListAdapter<PayData>() {
init {
addTypeAndLayout()
}
override fun addTypeAndLayout() {
mLayout[TYPE_HEAD] = R.layout.item_pay_head_layout
mLayout[TYPE_LIST] = R.layout.item_pay_layout
}
override fun isHasHead(): Boolean {
return true
}
override fun bindData(holder: BaseViewHolder<PayData>, data: PayData) {
if (holder.itemViewType== TYPE_HEAD) {
holder.itemView.setBackgroundColor(Color.parseColor("#FAD3D3"))
}else{
holder.itemView.setBackgroundColor(if (data.mType == 0) Color.RED else Color.YELLOW)
holder.setText(R.id.tv_hint,data.value)
}
}
}
BaseViewHolder
package com.niudong.lib_pays
import android.view.View
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.collection.SparseArrayCompat
import androidx.recyclerview.widget.RecyclerView
/**
* com.niudong.lib_pays.BaseViewHolder 处理通用Holder
*/
class BaseViewHolder<T>(
mItemView: View,
itemListClick: ((v: T, pos: Int) -> Boolean)?,
itemHeadClick: ((v: String) -> Unit)?
) : RecyclerView.ViewHolder(mItemView),
View.OnClickListener {
private var mViews: SparseArrayCompat<View>? = null
private var mData: T? = null
private var mView = mItemView
private var itemListClickListener = itemListClick
private var itemHeadClickListener = itemHeadClick
init {
mViews = SparseArrayCompat()
mView.setOnClickListener(this)
}
fun setData(data: T) {
this.mData = data
}
override fun onClick(v: View?) {
mData?.let {
val tag = v?.tag
if (tag == 0) {
itemHeadClickListener?.invoke("Click Head${adapterPosition}")
} else {
itemListClickListener?.invoke(it, adapterPosition)
}
}
}
/**
* 通过控件的Id获取对应的控件,如果没有则加入mViews,则从item根控件中查找并保存到mViews中
*/
private fun <V : View> findViewById(@IdRes viewId: Int): V {
var view: View? = mViews?.get(viewId)
if (view == null) {
view = itemView.findViewById(viewId)
mViews?.put(viewId, view)
}
return view as V
}
/**
* 设置对应id的控件的文本内容
*/
fun setText(@IdRes viewId: Int, text: CharSequence?): BaseViewHolder<T> {
getTextView(viewId).text = text
return this
}
/**
* 通过TextView的Id获取TextView
*
*/
private fun getTextView(@IdRes viewId: Int): TextView {
return findViewById(viewId)
}
}
网友评论