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.ItemGridBinding
import com.kaixed.kchat.model.HomeItem

/**
 * @Author: kaixed
 * @Date: 2024/10/27 18:30
 */
class MyGridAdapter(
    private val context: Context,
    private val items: List<HomeItem>
) : BaseAdapter() {

    override fun getCount(): Int {
        return items.size
    }

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

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

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        // 使用 View Binding
        val binding: ItemGridBinding = if (convertView == null) {
            ItemGridBinding.inflate(LayoutInflater.from(context), parent, false)
        } else {
            ItemGridBinding.bind(convertView)
        }

        val item = items[position]

        // 设置视图内容
        binding.textview.text = item.name
        binding.imageview.setImageResource(item.img)

        // 控制更多按钮的可见性
        binding.ivMore.visibility = if (item.more) View.VISIBLE else View.GONE

        return binding.root
    }
}
