1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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)
 
    }
}