PolicyFieldAdapter.kt 1.56 KB
package com.example.vip

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_policyfield.view.*

class PolicyFieldAdapter(private val items: ArrayList<PolicyFieldItem>) :
    RecyclerView.Adapter<PolicyFieldAdapter.ViewHolder>() {

    override fun getItemCount() = items.size

    override fun onBindViewHolder(holder: PolicyFieldAdapter.ViewHolder, position: Int) {
        val item = items[position]

        holder.apply {
            bind(item)
            itemView.tag = item
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
            PolicyFieldAdapter.ViewHolder {
        val inflatedView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_policyfield, parent, false)
        return PolicyFieldAdapter.ViewHolder(inflatedView)
    }

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {

        private var view: View = v

        fun bind(item: PolicyFieldItem) {
            view.fieldIcon.setImageDrawable(item.policyFieldItemImage)
            view.fieldIconText.text = item.policyFieldItemText
            view.setOnClickListener{

                val intent=Intent(view.context, RecommendActivity::class.java)
                //putExtraStart
                intent.putExtra("key",item.policyFieldItemText)
                //putExtraEnd
                view.context.startActivity(intent)
            }
        }
    }
}