【Android 入門指南】系列文章目錄:https://bit.ly/3TFgCzu
Android 線上課程教學影片:https://bit.ly/433Offj
本課程學習如何使用 RecyclerView 資料列表實作 Adapter。
若您無法順利觀看教學影片,請先登入您的 YouTube 帳號,然後點擊以下連結加入我們的頻道會員:
class SampleItemAdapter(private val items: List<SampleItem>) : RecyclerView.Adapter<SampleItemAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = ItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.bind(item)
}
override fun getItemCount(): Int {
return items.size
}
inner class ViewHolder(private val binding: ItemListBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: SampleItem) {
binding.itemTitle.text = item.title
binding.itemDesc.text = item.description
}
}
}