Created
January 25, 2020 00:57
-
-
Save danielmalone/66842509411784854bf76a20e012aabc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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