package com.example.firstapp.adapter
|
|
import android.graphics.Color
|
import android.graphics.Paint
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import android.widget.TextView
|
import androidx.cardview.widget.CardView
|
import androidx.recyclerview.widget.RecyclerView
|
import com.example.firstapp.R
|
import com.example.firstapp.model.CardData
|
|
|
class CardAdapter(private val cardList: List<CardData>,
|
private val itemClickListener: (MutableList<CardView>, CardData, CardView) -> Unit
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
|
private val CONTINUE_MONTHLY_VIEW_TYPE = 0
|
private val YEARLY_VIEW_TYPE = 1
|
private val SINGLE_MONTH_VIEW_TYPE = 2
|
|
private val cardViewList: MutableList<CardView> = mutableListOf()
|
|
override fun getItemViewType(position: Int): Int {
|
return when (cardList[position]) {
|
is CardData.ContinueMonthly -> CONTINUE_MONTHLY_VIEW_TYPE
|
is CardData.Yearly -> YEARLY_VIEW_TYPE
|
is CardData.SingleMonth -> SINGLE_MONTH_VIEW_TYPE
|
}
|
}
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
return when (viewType) {
|
CONTINUE_MONTHLY_VIEW_TYPE -> {
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_card_continue_monthly, parent, false)
|
ContinueMonthlyViewHolder(view)
|
}
|
YEARLY_VIEW_TYPE -> {
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_card_yearly, parent, false)
|
YearlyViewHolder(view)
|
}
|
SINGLE_MONTH_VIEW_TYPE -> {
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_card_single_month, parent, false)
|
SingleMonthViewHolder(view)
|
}
|
else -> throw IllegalArgumentException("Unknown view type")
|
}
|
}
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
when (holder) {
|
is ContinueMonthlyViewHolder -> {
|
val data = cardList[position] as CardData.ContinueMonthly
|
|
holder.firstMonthTag.text = data.firstMonthTag
|
holder.titleText.text = data.title
|
holder.subTitleText.text = data.subTitle
|
holder.priceText.text = data.price
|
holder.nextPriceText.text = data.nextPrice
|
holder.autoRenewText.text = data.autoRenew
|
|
cardViewList.add(holder.cardView)
|
|
holder.itemView.setOnClickListener { itemClickListener(cardViewList,data, holder.cardView) } // 设置点击事件
|
}
|
is YearlyViewHolder -> {
|
val data = cardList[position] as CardData.Yearly
|
holder.discountTag.text = data.discountTag
|
holder.titleText.text = data.title
|
holder.priceText.text = data.price
|
holder.originalPriceText.text = data.originalPrice
|
|
// 设置中划线
|
val paint = holder.originalPriceText.paint
|
paint.flags = paint.flags or Paint.STRIKE_THRU_TEXT_FLAG
|
paint.color = Color.RED // 设置中划线的颜色
|
|
cardViewList.add(holder.cardView)
|
|
holder.itemView.setOnClickListener { itemClickListener(cardViewList,data, holder.cardView) } // 设置点击事件
|
}
|
is SingleMonthViewHolder -> {
|
val data = cardList[position] as CardData.SingleMonth
|
holder.titleText.text = data.title
|
holder.priceText.text = data.price
|
|
cardViewList.add(holder.cardView)
|
|
holder.itemView.setOnClickListener { itemClickListener(cardViewList,data, holder.cardView) } // 设置点击事件
|
|
}
|
}
|
}
|
|
override fun getItemCount(): Int {
|
return cardList.size
|
}
|
|
inner class ContinueMonthlyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
val cardView: CardView = view.findViewById(R.id.vip_first_month_card_view)
|
val firstMonthTag: TextView = view.findViewById(R.id.first_month_tag)
|
val titleText: TextView = view.findViewById(R.id.title_text)
|
val subTitleText: TextView = view.findViewById(R.id.sub_title_text)
|
val priceText: TextView = view.findViewById(R.id.price_text)
|
val nextPriceText: TextView = view.findViewById(R.id.next_price_text)
|
val autoRenewText: TextView = view.findViewById(R.id.auto_renew_text)
|
|
}
|
|
inner class YearlyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
val cardView: CardView = view.findViewById(R.id.vip_year_card_view)
|
val discountTag: TextView = view.findViewById(R.id.discount_tag)
|
val titleText: TextView = view.findViewById(R.id.yearly_title_text)
|
val priceText: TextView = view.findViewById(R.id.yearly_price_text)
|
val originalPriceText: TextView = view.findViewById(R.id.yearly_original_price_text)
|
|
}
|
|
inner class SingleMonthViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
val cardView: CardView = view.findViewById(R.id.vip_month_card_view)
|
val titleText: TextView = view.findViewById(R.id.single_month_title_text)
|
val priceText: TextView = view.findViewById(R.id.single_month_price_text)
|
|
}
|
}
|