Skip to content

Instantly share code, notes, and snippets.

@danielmalone
Created January 25, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmalone/66842509411784854bf76a20e012aabc to your computer and use it in GitHub Desktop.
Save danielmalone/66842509411784854bf76a20e012aabc to your computer and use it in GitHub Desktop.
package com.danielmalone.dansinvoicing
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.invoice_row.view.*
class InvoicesAdapter : ListAdapter<Invoice, InvoicesAdapter.ViewHolder>(InvoicesDiff()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater.inflate(R.layout.invoice_row, parent, false))
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(invoice: Invoice) {
itemView.invoiceTitle.text = invoice.title
itemView.invoiceClient.text = invoice.client
itemView.invoiceDate.text = invoice.date
}
}
}
class InvoicesDiff : DiffUtil.ItemCallback<Invoice>() {
override fun areItemsTheSame(oldItem: Invoice, newItem: Invoice): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: Invoice, newItem: Invoice): Boolean {
return areItemsTheSame(oldItem, newItem)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment