package com.ytpm.hydtw.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.ytpm.hydtw.dao.item

abstract class BaseAdapter<VB : ViewBinding, T>(
    val inflater: (LayoutInflater, parent: ViewGroup, attachToRoot: Boolean) -> VB,
    val data: MutableList<T>,
) : RecyclerView.Adapter<BaseHolder<VB>>() {


    private var mContext: Context? = null

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder<VB> {
        mContext = parent.context
        return BaseHolder(inflater(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int = data.size

    override fun onBindViewHolder(holder: BaseHolder<VB>, position: Int) {
        mContext?.let { onBindingData(it, holder, holder.binding, data[position], position) }
    }

    protected abstract fun onBindingData(
        context: Context,
        holder: BaseHolder<VB>,
        binding: VB,
        t: T,
        position: Int
    )

}