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()
|
}
|
|
}
|