package com.dahe.gldriver.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.dahe.gldriver.R
import com.dahe.gldriver.bean.GridBean


/**
 * @ClassName GridItemAdapter
 * @Author 用户
 * @Date 2024/1/23 10:26
 * @Description TODO
 */
class GridItemAdapter(private val context: Context, private val dataList: MutableList<GridBean>) :
    BaseAdapter() {

    private var cusLayout: Int = 0

    constructor(context: Context, dataList: MutableList<GridBean>, mlayoutid: Int) : this(
        context,
        dataList
    ) {
        this.cusLayout = mlayoutid
    }

    // 实现必要的方法...
    override fun getCount(): Int {
        return dataList.size
    }

    override fun getItem(position: Int): Any {
        return dataList[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var view = convertView
        var holder: ViewHolder
        if (convertView == null) {

            view = LayoutInflater.from(context)
                .inflate(if (cusLayout == 0) R.layout.grid_item else cusLayout, null)
            holder = ViewHolder()
            holder.title = view.findViewById(R.id.title)
            holder.image = view.findViewById(R.id.image)
            view?.tag = holder
        } else {
            holder = (view?.tag) as ViewHolder
        }
        val item = dataList[position]
        holder.title?.text = item.title
        holder.image?.setBackgroundResource(item.image)

        return view!!
    }

    private class ViewHolder {
        var title: TextView? = null
        var image: ImageView? = null
    }
}

