package com.kaixed.kchat.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.kaixed.kchat.databinding.FunctionGridItemBinding
import com.kaixed.kchat.model.FunctionItem

/**
 * @Author: kaixed
 * @Date: 2024/10/27 18:29
 */
class FunctionPanelAdapter(
    private val items: List<FunctionItem>,
    private val context: Context
) :
    BaseAdapter() {
    override fun getCount(): Int = items.size
    override fun getItem(position: Int): Any? {
        return null
    }

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

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView = convertView
        val binding: FunctionGridItemBinding

        if (convertView == null) {
            // convertView 为空时，说明需要创建新的视图
            binding = FunctionGridItemBinding.inflate(LayoutInflater.from(context), parent, false)
            convertView = binding.root
            // 将 binding 存入 tag，以后可复用
            convertView.tag = binding
        } else {
            // convertView 不为空时，直接复用已有的 View
            binding = convertView.tag as FunctionGridItemBinding
        }
        binding.tvItemName.text = items[position].itemName
        binding.ivItem.setImageResource(items[position].iconResId)
        return convertView
    }
}
