Skip to content

Instantly share code, notes, and snippets.

@ajailani4
Created June 3, 2021 11:17
Show Gist options
  • Save ajailani4/f770d1d81d5e1fc8cc13d8ffbd70867b to your computer and use it in GitHub Desktop.
Save ajailani4/f770d1d81d5e1fc8cc13d8ffbd70867b to your computer and use it in GitHub Desktop.
class PhonesHomeAdapter(
private val phonesHomeList: List<Phone>?,
private val listener: (String) -> Unit
) : RecyclerView.Adapter<PhonesHomeAdapter.ViewHolder>() {
private lateinit var binding: ItemPhoneHomeBinding
class ViewHolder(private val binding: ItemPhoneHomeBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(phone: Phone?, listener: (String) -> Unit) {
binding.apply {
Glide.with(image.context)
.load(phone?.image)
.into(image)
name.text = phone?.name
root.setOnClickListener {
listener(phone!!.slug)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding = ItemPhoneHomeBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(phonesHomeList?.get(position), listener)
}
override fun getItemCount() = phonesHomeList!!.size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment