| New file | 
|  |  |  | 
|---|
|  |  |  | package com.dayu.general.adapter | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import android.view.LayoutInflater | 
|---|
|  |  |  | import android.view.View | 
|---|
|  |  |  | import android.view.ViewGroup | 
|---|
|  |  |  | import androidx.recyclerview.widget.RecyclerView | 
|---|
|  |  |  | import com.dayu.general.bean.net.SearchCardResult | 
|---|
|  |  |  | import com.dayu.general.databinding.ItemCardListBinding | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * Description: 卡片列表适配器 | 
|---|
|  |  |  | * Author: zuo | 
|---|
|  |  |  | * Date: 2025/3/31 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | class CardListAdapter : RecyclerView.Adapter<CardListAdapter.CardViewHolder>() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private val cardList = mutableListOf<SearchCardResult.CardInfo>() | 
|---|
|  |  |  | private var onItemClickListener: ((SearchCardResult.CardInfo) -> Unit)? = null | 
|---|
|  |  |  |  | 
|---|
|  |  |  | fun setData(cards: List<SearchCardResult.CardInfo>) { | 
|---|
|  |  |  | cardList.clear() | 
|---|
|  |  |  | cardList.addAll(cards) | 
|---|
|  |  |  | notifyDataSetChanged() | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | fun addData(cards: List<SearchCardResult.CardInfo>) { | 
|---|
|  |  |  | val startPosition = cardList.size | 
|---|
|  |  |  | cardList.addAll(cards) | 
|---|
|  |  |  | notifyItemRangeInserted(startPosition, cards.size) | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | fun setOnItemClickListener(listener: (SearchCardResult.CardInfo) -> Unit) { | 
|---|
|  |  |  | this.onItemClickListener = listener | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder { | 
|---|
|  |  |  | val binding = ItemCardListBinding.inflate(LayoutInflater.from(parent.context), parent, false) | 
|---|
|  |  |  | return CardViewHolder(binding) | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | override fun onBindViewHolder(holder: CardViewHolder, position: Int) { | 
|---|
|  |  |  | val card = cardList[position] | 
|---|
|  |  |  | holder.bind(card) | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | override fun getItemCount(): Int = cardList.size | 
|---|
|  |  |  |  | 
|---|
|  |  |  | inner class CardViewHolder(private val binding: ItemCardListBinding) : RecyclerView.ViewHolder(binding.root) { | 
|---|
|  |  |  | fun bind(card: SearchCardResult.CardInfo) { | 
|---|
|  |  |  | // 设置数据显示,添加判空处理 | 
|---|
|  |  |  | binding.tvCardNumber.text = "卡号:${card.cardNum ?: "无"}" | 
|---|
|  |  |  | binding.tvCardType.text = "卡类型:${card.cardType ?: "未知"}" | 
|---|
|  |  |  | binding.tvCardStatus.text = "状态:${card.stateName ?: "未知"}" | 
|---|
|  |  |  | binding.tvClientName.text = "姓名:${card.clientName ?: "未知"}" | 
|---|
|  |  |  | binding.tvClientNum.text = "客户编号:${card.clientNum ?: "无"}" | 
|---|
|  |  |  | binding.tvCardBalance.text = "余额:${card.money ?: "0.00"} 元" | 
|---|
|  |  |  | binding.tvPhone.text = "电话:${formatPhone(card.phone)}" | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 显示身份证号 | 
|---|
|  |  |  | card.idCard?.let { idCard -> | 
|---|
|  |  |  | if (idCard.isNotEmpty()) { | 
|---|
|  |  |  | binding.tvIdCard.text = "身份证:${formatIdCard(idCard)}" | 
|---|
|  |  |  | binding.tvIdCard.visibility = View.VISIBLE | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | binding.tvIdCard.visibility = View.GONE | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } ?: run { | 
|---|
|  |  |  | binding.tvIdCard.visibility = View.GONE | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | binding.root.setOnClickListener { | 
|---|
|  |  |  | onItemClickListener?.invoke(card) | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 格式化身份证号,保护隐私 | 
|---|
|  |  |  | private fun formatIdCard(idCard: String?): String { | 
|---|
|  |  |  | return if (!idCard.isNullOrEmpty() && idCard.length >= 18) { | 
|---|
|  |  |  | val start = idCard.substring(0, 6) | 
|---|
|  |  |  | val end = idCard.substring(idCard.length - 4) | 
|---|
|  |  |  | "$start****$end" | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | idCard ?: "无" | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 格式化手机号,保护隐私 | 
|---|
|  |  |  | private fun formatPhone(phone: String?): String { | 
|---|
|  |  |  | return if (!phone.isNullOrEmpty() && phone.length >= 11) { | 
|---|
|  |  |  | val start = phone.substring(0, 3) | 
|---|
|  |  |  | val end = phone.substring(phone.length - 4) | 
|---|
|  |  |  | "$start****$end" | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | phone ?: "无" | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|