Skip to content

Instantly share code, notes, and snippets.

@cdmunoz
Created March 21, 2018 01:02
Show Gist options
  • Save cdmunoz/37b4a6a3ee75c387f1c00ae84712c63a to your computer and use it in GitHub Desktop.
Save cdmunoz/37b4a6a3ee75c387f1c00ae84712c63a to your computer and use it in GitHub Desktop.
class CryptocurrenciesAdapter(
cryptocurrencies: List<Cryptocurrency>?) : RecyclerView.Adapter<CryptocurrencieViewHolder>() {
private var cryptocurrenciesList = ArrayList<Cryptocurrency>()
init {
this.cryptocurrenciesList = cryptocurrencies as ArrayList<Cryptocurrency>
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CryptocurrencieViewHolder {
val itemView = LayoutInflater.from(parent?.context).inflate(R.layout.cryptocurrency_list_item,
parent, false)
return CryptocurrencieViewHolder(itemView)
}
override fun getItemCount(): Int {
return cryptocurrenciesList.size
}
override fun onBindViewHolder(holder: CryptocurrencieViewHolder?, position: Int) {
val cryptocurrencyItem = cryptocurrenciesList[position]
holder?.cryptocurrencyListItem(cryptocurrencyItem)
}
fun addCryptocurrencies(cryptocurrencies: List<Cryptocurrency>){
val initPosition = cryptocurrenciesList.size
cryptocurrenciesList.addAll(cryptocurrencies)
notifyItemRangeInserted(initPosition, cryptocurrenciesList.size)
}
class CryptocurrencieViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var cryptocurrencyId = itemView.findViewById<TextView>(R.id.cryptocurrency_id)
fun cryptocurrencyListItem(cryptocurrencyItem: Cryptocurrency) {
cryptocurrencyId.text = cryptocurrencyItem.id
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment