Skip to content

Instantly share code, notes, and snippets.

@KryptKode
Created October 11, 2019 07:35
Show Gist options
  • Save KryptKode/d6bd03badacd7322318367e6c75ebad8 to your computer and use it in GitHub Desktop.
Save KryptKode/d6bd03badacd7322318367e6c75ebad8 to your computer and use it in GitHub Desktop.
abstract class BaseViewHolder<T>(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun performBind(item :T?)
}
class ItemViewHolderPost constructor(val binding: ItemBinding)
: BaseViewHolder<Model>(binding.root) {
override fun performBind(item: Model?) {
//TODO: Bind to view
}
}
class WordpressAdapter()
: PagedListAdapter<Any, BaseViewHolder<Any>>(ITEM_CALLBACK_OBJ) {
companion object {
const val POST = 1
const val NATIVE_AD = 5
const val NUM_OF_POST_BEFORE_ADS = 4
val ITEM_CALLBACK_OBJ: DiffUtil.ItemCallback<Any> = object : DiffUtil.ItemCallback<Any>() {
override fun areItemsTheSame(oldItem: Any, newItem: Any): Boolean {
return if(oldItem is Model && newItem is Model){
oldItem.date == (newItem.date)
}else{
false
}
}
override fun areContentsTheSame(oldItem: Any, newItem: Any): Boolean {
return if(oldItem is Model && newItem is Model){
(oldItem as Model) == (newItem as Model)
}else{
false
}
}
}
}
private val nativeAds = mutableListOf<UnifiedNativeAd>()
fun addNativeAd(ad:UnifiedNativeAd){
nativeAds.add(ad)
}
override fun getItemViewType(position: Int): Int {
val nativeAdRows = getNativeAdRows()
return if(hasNativeAds() && nativeAdRows.contains(position)){
NATIVE_AD
}else {
POST
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<Any> {
return when (viewType) {
NATIVE_AD -> NativeAdViewHolder(ItemNativeAdBinding.inflate(LayoutInflater.from(parent.context), parent, false)) as BaseViewHolder<Any>
else -> ItemViewHolderPost(ListviewRowBinding.inflate(LayoutInflater.from(parent.context), parent, false), listener) as BaseViewHolder<Any>
}
}
override fun onBindViewHolder(holder: BaseViewHolder<Any>, position: Int) {
when (getItemViewType(position)) {
NATIVE_AD -> {
val adPosition = position * nativeAds.size / itemCount
val nativeAd = nativeAds[adPosition]
holder.performBind(nativeAd)
}
else -> holder.performBind(getItem(position))
}
}
fun onNativeAdLoaded() {
val rows = getNativeAdRows()
rows.forEach {
notifyItemInserted(it)
}
}
private fun hasNativeAds(): Boolean {
return nativeAds.isNotEmpty()
}
private fun getNativeAdRows(): List<Int>{
val rows = mutableListOf<Int>()
for (i in 0 .. itemCount){
if(i != 0 && i % NUM_OF_POST_BEFORE_ADS == 0){
rows.add(i)
}
}
return rows
}
}
class NativeAdViewHolder (private val binding: ItemNativeAdBinding) :
BaseViewHolder<UnifiedNativeAd>(binding.root){
override fun performBind(item: UnifiedNativeAd?) {
//TODO: Bind
}
}
@adhavanj
Copy link

adhavanj commented Apr 2, 2020

Guess this might override the Item, like this

colors

My suggestion is to modify the data source and Ad item view in the source.

@KryptKode
Copy link
Author

Yes, you're right. It would override some. But can one modify paging 2.0 data after it's been created? I think that's only possible in paging 3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment