Skip to content

Instantly share code, notes, and snippets.

@ajailani4
Created June 3, 2021 11:11
Show Gist options
  • Save ajailani4/96ddb2cac4e5424c90b150efad2ced4d to your computer and use it in GitHub Desktop.
Save ajailani4/96ddb2cac4e5424c90b150efad2ced4d to your computer and use it in GitHub Desktop.
class BrandsHomeAdapter(
private val moreListener: (Brand) -> Unit,
private val phoneListener: (String, String) -> Unit
) : PagingDataAdapter<Brand, BrandsHomeAdapter.ViewHolder>(DataDifferentiator) {
private lateinit var binding: ItemBrandHomeBinding
private lateinit var phonesHomeAdapter: PhonesHomeAdapter
private val rvViewPool = RecyclerView.RecycledViewPool()
object DataDifferentiator : DiffUtil.ItemCallback<Brand>() {
override fun areItemsTheSame(oldItem: Brand, newItem: Brand): Boolean {
return oldItem.slug == newItem.slug
}
override fun areContentsTheSame(oldItem: Brand, newItem: Brand): Boolean {
return oldItem == oldItem
}
}
class ViewHolder(private val binding: ItemBrandHomeBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(brand: Brand, moreListener: (Brand) -> Unit) {
binding.apply {
name.text = brand.name
moreIv.setOnClickListener {
moreListener(brand)
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
binding = ItemBrandHomeBinding.inflate(
LayoutInflater.from(parent.context), parent, false
)
return ViewHolder(binding)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
getItem(position)?.let { brand ->
holder.bind(brand, moreListener)
// Setup phonesHomeAdapter and phonesHomeRv
phonesHomeAdapter = PhonesHomeAdapter(brand.phonesList) { phoneSlug ->
phoneListener(brand.slug, phoneSlug)
}
binding.phonesHomeRv.apply {
layoutManager = LinearLayoutManager(
context, LinearLayoutManager.HORIZONTAL, false
)
adapter = phonesHomeAdapter
setRecycledViewPool(rvViewPool)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment