Created
September 10, 2020 01:55
-
-
Save carolinemusyoka/e7508cf3f6a2dc749805dd6aab41ebf5 to your computer and use it in GitHub Desktop.
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
class MainAdapter(private val result: ArrayList<Hero>, | |
private val clickListener: (Long) -> Unit): | |
RecyclerView.Adapter<MainAdapter.DataViewHolder>() { | |
class DataViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
fun bind(hero: Hero, clickListener: (Long) -> Unit ) { | |
itemView.apply { | |
superheroName.text = hero.name | |
Glide.with(superheroImage.context) | |
.load(hero.image.lg) | |
.into(superheroImage) | |
// superheroName.setOnClickListener { clickListener(hero.id) } | |
// superheroImage.setOnClickListener { clickListener(hero.id) } | |
} | |
} | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DataViewHolder = | |
DataViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_superhero, parent, false)) | |
override fun getItemCount(): Int = result.size | |
override fun onBindViewHolder(holder: DataViewHolder, position: Int) { | |
holder.bind(result[position],clickListener) | |
} | |
fun addSuperHeroes(hero: List<Hero>) { | |
this.result.apply { | |
clear() | |
addAll(hero) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment