| | |
| | | package com.example.firstapp.adapter |
| | | |
| | | import android.content.Context |
| | | import android.text.TextUtils |
| | | import android.view.LayoutInflater |
| | | import android.view.View |
| | | import android.view.ViewGroup |
| | |
| | | import androidx.recyclerview.widget.RecyclerView |
| | | import com.example.firstapp.R |
| | | import com.example.firstapp.entity.InvitationRecord |
| | | import com.example.firstapp.utils.Log |
| | | |
| | | class InvitationAdapter : RecyclerView.Adapter<InvitationAdapter.ViewHolder>() { |
| | | |
| | | private var records = emptyList<InvitationRecord>() |
| | | class InvitationAdapter( |
| | | private val context: Context, |
| | | private val data: List<InvitationRecord>, |
| | | ) : RecyclerView.Adapter<InvitationAdapter.InvitationViewHolder>() { |
| | | |
| | | class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
| | | val message: TextView = itemView.findViewById(R.id.message) |
| | | } |
| | | |
| | | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { |
| | | val view = LayoutInflater.from(parent.context) |
| | | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InvitationViewHolder { |
| | | val view = LayoutInflater.from(context) |
| | | .inflate(R.layout.activity_invitation_success, parent, false) |
| | | return ViewHolder(view) |
| | | } |
| | | |
| | | override fun onBindViewHolder(holder: ViewHolder, position: Int) { |
| | | val record = records[position] |
| | | return InvitationViewHolder(view) |
| | | } |
| | | override fun onBindViewHolder(holder: InvitationViewHolder, position: Int) { |
| | | val record = data[position % data.size] // 使用取模实现循环 |
| | | holder.message.text = "${record.userName}邀请好友,${record.reward}" |
| | | |
| | | } |
| | | |
| | | override fun getItemCount() = records.size |
| | | override fun getItemCount() = if (data.isEmpty()) 0 else Int.MAX_VALUE |
| | | |
| | | fun submitList(newList: List<InvitationRecord>) { |
| | | records = newList |
| | | notifyDataSetChanged() |
| | | class InvitationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { |
| | | val message: TextView = itemView.findViewById(R.id.message) |
| | | } |
| | | } |