Created
December 6, 2020 01:53
-
-
Save oneSIX/d6accb477563ab915627d12415e1b345 to your computer and use it in GitHub Desktop.
This gist can be used as an example of a multi-item RecyclerView adapter using ViewBinding and a Sealed Class to represent the ViewHolder types.
This file contains hidden or 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.mobile.devpackage.bll.adapters | |
import android.content.Context | |
import android.view.LayoutInflater | |
import android.view.View.* | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.RecyclerView | |
import com.mobile.devname.R | |
import com.mobile.devname.databinding.GenericTransactionRowFooterLayoutBinding | |
import com.mobile.devname.databinding.GenericTransactionRowHeaderLayoutBinding | |
import com.mobile.devname.databinding.GenericTransactionRowLayoutBinding | |
import com.mobile.devname.bll.models.Transaction | |
import com.mobile.devname.bll.utilities.UserSettings | |
const val TYPE_HEADER = 0 | |
const val TYPE_TRANSACION = 1 | |
const val TYPE_FOOTER = 2 | |
class ExampleAdapter( | |
private val context: Context, | |
private val listener: OnTransactionClick | |
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
private lateinit var transactions: ArrayList<TransListItem> | |
interface OnTransactionClick { | |
fun onTransactionClick(transaction: TransListItem) | |
fun onCheckClick(transaction: TransListItem) | |
} | |
fun submitListItems(items: ArrayList<TransListItem>){ | |
transactions = items | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
return when (viewType) { | |
TYPE_TRANSACION -> TransactionViewHolder.from(parent) | |
TYPE_HEADER -> HeaderViewHolder.from(parent) | |
TYPE_FOOTER -> FooterViewHolder.from(parent) | |
else -> { | |
throw throw ClassCastException("Unknown viewType $viewType") | |
} | |
} | |
} | |
override fun getItemViewType(position: Int): Int { | |
return transactions[position].getType() | |
} | |
override fun getItemCount(): Int { | |
return transactions.count() | |
} | |
class TransactionViewHolder private constructor(val binding: GenericTransactionRowLayoutBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bind(item: TransListItem, listener: OnTransactionClick, context: Context) { | |
val transaction = item.getTransaction() | |
binding.balancetext.text = transaction.amount | |
binding.account.text = transaction.description | |
if (transaction.amount.contains("-")) { | |
binding.balancetext.setTextColor(context.resources.getColor(R.color.red, null)) | |
} else { | |
binding.balancetext.setTextColor( | |
context.resources.getColor( | |
R.color.darkgreen, | |
null | |
) | |
) | |
} | |
if(transaction.checkNumber.isEmpty()){ | |
// hide check view | |
binding.checkview.visibility = GONE | |
} else { | |
// show check view + set listener | |
binding.checkview.visibility = VISIBLE | |
binding.checkview.setOnClickListener { | |
listener.onCheckClick(item) | |
} | |
} | |
binding.genericRowLayout.setOnClickListener { | |
listener.onTransactionClick(item) | |
} | |
} | |
companion object { | |
fun from(parent: ViewGroup): TransactionViewHolder { | |
val layoutInflater = LayoutInflater.from(parent.context) | |
val binding = | |
GenericTransactionRowLayoutBinding.inflate(layoutInflater, parent, false) | |
return TransactionViewHolder(binding) | |
} | |
} | |
} | |
class HeaderViewHolder private constructor(val binding: GenericTransactionRowHeaderLayoutBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bind(item: TransListItem) { | |
binding.account.text = item.getTransaction().postDate | |
} | |
companion object { | |
fun from(parent: ViewGroup): HeaderViewHolder { | |
val layoutInflater = LayoutInflater.from(parent.context) | |
val binding = | |
GenericTransactionRowHeaderLayoutBinding.inflate(layoutInflater, parent, false) | |
return HeaderViewHolder(binding) | |
} | |
} | |
} | |
class FooterViewHolder private constructor(val binding: GenericTransactionRowFooterLayoutBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bind(item: TransListItem, listener: OnTransactionClick, context: Context) { | |
val incrementDays = UserSettings(context).getPrefsInt(UserSettings.ADDITIONAL_TRANS_DOWNLOAD) | |
val previousText = "Previous $incrementDays Days" | |
binding.account.text = previousText | |
binding.genericRowLayout.setOnClickListener { | |
listener.onTransactionClick(item) | |
} | |
} | |
companion object { | |
fun from(parent: ViewGroup): FooterViewHolder { | |
val layoutInflater = LayoutInflater.from(parent.context) | |
val binding = | |
GenericTransactionRowFooterLayoutBinding.inflate(layoutInflater, parent, false) | |
return FooterViewHolder(binding) | |
} | |
} | |
} | |
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | |
when (getItemViewType(position)) { | |
TYPE_HEADER -> { | |
val headerViewHolder = holder as HeaderViewHolder | |
headerViewHolder.bind(transactions[position]) | |
} | |
TYPE_TRANSACION -> { | |
val transactionViewHolder = holder as TransactionViewHolder | |
transactionViewHolder.bind(transactions[position], listener, context) | |
} | |
TYPE_FOOTER -> { | |
val footerViewHolder = holder as FooterViewHolder | |
footerViewHolder.bind(transactions[position], listener, context) | |
} | |
} | |
} | |
sealed class TransListItem { | |
data class TransactionItem(private val transaction: Transaction) : TransListItem() { | |
override fun getType(): Int { | |
return TYPE_TRANSACION | |
} | |
override fun getTransaction(): Transaction { | |
return transaction | |
} | |
} | |
data class HeaderItem(private val transaction: Transaction) : TransListItem() { | |
override fun getType(): Int { | |
return TYPE_HEADER | |
} | |
override fun getTransaction(): Transaction { | |
return transaction | |
} | |
} | |
data class FooterItem(private val transaction: Transaction) : TransListItem() { | |
override fun getType(): Int { | |
return TYPE_FOOTER | |
} | |
override fun getTransaction(): Transaction { | |
return transaction | |
} | |
} | |
abstract fun getType(): Int | |
abstract fun getTransaction(): Transaction | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment