通用的recyclerViewAdapter(kotlin版)

立即上代码

public abstract class BaseRecyclerAdapter(val datas: ArrayList?, val ctx: Context?) : RecyclerView.Adapter() {
    override fun getItemCount(): Int {
        return datas?.size ?: 0
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder? {
        var view = getViewResource(viewType)
        var holder = BaseViewHolder(view)
        addAllViewItems(holder?.viewMap,view)
        return holder
    }

    override fun onBindViewHolder(holder: BaseViewHolder?, position: Int) {
        bindData(holder?.viewMap, datas?.get(position), position)
    }

    /**
     * 添加item布局
     */
    abstract fun getViewResource(viewType: Int): View

    /**
     * 记录下item布局下所有控件
     */
    abstract fun addAllViewItems(viewMap: SparseArray?,root:View)

    /**
     * 绑定item数据
     */
    abstract fun  bindData(viewMap: SparseArray?, t: T?, position: Int)
}

public class BaseViewHolder : RecyclerView.ViewHolder {
    var viewMap= SparseArray()
    constructor(itemView: View?) : super(itemView)
}

你可能感兴趣的:(通用的recyclerViewAdapter(kotlin版))