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
package com.example.firstapp.activity
 
import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.navigation.ui.AppBarConfiguration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.firstapp.R
import com.example.firstapp.adapter.CardAdapter
import com.example.firstapp.databinding.ActivityVipBinding
import com.example.firstapp.entity.CardData
import com.example.firstapp.utils.Log
 
class VipActivity : AppCompatActivity() {
 
    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityVipBinding
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        binding = ActivityVipBinding.inflate(layoutInflater)
        setContentView(binding.root)
 
        setSupportActionBar(binding.toolbar)
 
        // 显式设置 MaterialToolbar 的标题
        supportActionBar?.title = ""
 
        // 如果不需要 ActionBar 的导航功能,可以禁用
//        supportActionBar?.setDisplayHomeAsUpEnabled(false)
//        supportActionBar?.setDisplayShowHomeEnabled(false)
 
 
        // 返回事件
        binding.ivBack.setOnClickListener{
            finish()
        }
 
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
 
        val cardList = listOf(
            CardData.ContinueMonthly(
                "首月限时特惠",
                "连续包月",
                "首次开通",
                "¥9.9",
                "次月起12元/月",
                "自动续费可随时取消"
            ),
            CardData.Yearly(
                "折合9元/月",
                "年卡",
                "¥108/年",
                "¥168"
            ),
            CardData.SingleMonth(
                "1个月",
                "¥15"
            )
        )
 
        val adapter = CardAdapter(cardList) {cardViewList, cardData, cardView ->
            handleCardClick(cardViewList,cardData,cardView)
        }
 
        recyclerView.adapter = adapter
 
 
        try {
            val firstMonthCardView = findViewById<CardView>(R.id.vip_first_month_card_view)
            firstMonthCardView.foreground = ContextCompat.getDrawable(this, R.drawable.gray_border_shape)  // 设置自定义背景
 
        }catch (e: Exception){
            e.stackTrace
            Log.e("VipActivity",e.message+"")
        }
 
    }
 
    /**
     * 会员卡样式点击事件
     */
    private fun handleCardClick(cardViewList: MutableList<CardView>,cardData: CardData, cardView: CardView) {
        // 处理点击事件,修改样式
        // 修改所有的边框色都是灰色
        cardViewList.forEach { card ->
            card.foreground = ContextCompat.getDrawable(this, R.drawable.gray_border_shape)  // 设置自定义背景
        }
 
        // 单独将点击的卡片边框颜色改为金色
//            cardView.setCardBackgroundColor(Color.parseColor("#FF0000")) // 举例:设置背景颜色
        cardView.foreground = ContextCompat.getDrawable(this, R.drawable.gold_border_shape)  // 设置自定义背景
        val title = when (cardData) {
            is CardData.ContinueMonthly -> cardData.title
            is CardData.Yearly -> cardData.title
            is CardData.SingleMonth -> cardData.title
        }
        // 处理点击事件,这里我们只是简单地展示一个 Toast
        Toast.makeText(this, "点击了: ${title}", Toast.LENGTH_SHORT).show()
    }
 
}