Created
June 3, 2021 11:11
-
-
Save ajailani4/96ddb2cac4e5424c90b150efad2ced4d to your computer and use it in GitHub Desktop.
This file contains 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 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