package com.casic.br.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.casic.br.R

/**
 * 文字列表Adapter
 * */
class IconTextListAdapter(
    private val context: Context,
    private val titles: Array<String>
) : RecyclerView.Adapter<IconTextListAdapter.ItemViewHolder>() {

    private var layoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ItemViewHolder(
        layoutInflater.inflate(R.layout.item_icon_text_rv_l, parent, false)
    )

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.textView.text = titles[position]
        holder.itemView.setOnClickListener {
            clickListener?.onItemClick(position)
        }
    }

    override fun getItemCount(): Int = titles.size

    private var clickListener: OnListItemClickListener? = null

    interface OnListItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnGridItemClickListener(clickListener: OnListItemClickListener?) {
        this.clickListener = clickListener
    }

    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageView)
        val textView: TextView = itemView.findViewById(R.id.textView)
    }
}