I tried implementing ADS in recycler view every 3 items but instead of following the next items in the list of items, it repeats the first 3 times over and over until the list.size() is over. AdMob , ad item in Recycler View hides some items and creates spaces after the ad For achieving this, i tried to implement this code abovein my own code and it works fine except for the repetition of the first 3 items. The code is modified to satisfy my needs, so it may seem strange.
open class AdsAdapter2(override val activity: FragmentActivity, var dataSet: MutableList<adslist>, protected var itemLayoutRes: Int,
showSectionName: Boolean = true) : AbsMultiSelectAdapter<AdsAdapter2.ViewHolder, adslist>(activity, R.menu.menu_media_selection),
PopupTextProvider {
var onItemClick: ((adslist) -> Unit)? = null
private var showSectionName = true
init {
this.showSectionName = showSectionName
this.setHasStableIds(true)
}
open fun swapDataSet(dataSet: List<adslist>) {
this.dataSet = ArrayList(dataSet)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
if (viewType == AD_VIEWTYPE){
val v = LayoutInflater.from(activity).inflate(R.layout.item_favourite_card, parent, false)
return ViewHolder(v)
}
else{
val v = LayoutInflater.from(activity).inflate(itemLayoutRes, parent, false)
return ViewHolder(v)
}
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val card = getCardForPosition(position)
if (card == null){
Picasso.get().load(R.drawable.default_artist_art).into(holder.image)
} else {
// holder.courseNameTV.text = song.artistandname
if (card.img == 0) {
Picasso.get().load(card.img2).into(holder.img)
} else {
Picasso.get().load(card.img).into(holder.img)
}
}
}
override fun getItemCount() = dataSet.size + (dataSet.size / ITEMS_PER_AD)
override fun getIdentifier(position: Int): adslist? {
return getCardForPosition(position)
}
open inner class ViewHolder(itemView: View) : MediaEntryViewHolder(itemView) {
protected open val adslist: adslist
get() = dataSet[layoutPosition]
// val layoutposition: LinearLayout = itemView.findViewById(R.id.layoutposition)
init {
itemView.setOnClickListener {
onItemClick?.invoke(getCardForPosition(position)!!) //WHEN AD CLICKED ==NULL
}
}
val img: AppCompatImageView = itemView.findViewById(R.id.image)
val layput: MaterialCardView = itemView.findViewById(R.id.imageContainer)
// val layoutposition: LinearLayout = itemView.findViewById(R.id.layoutposition)
val layout: MaterialCardView = itemView.findViewById(R.id.imageContainer)
val image: AppCompatImageView = itemView.findViewById(R.id.image)
val title: MaterialTextView = itemView.findViewById(R.id.title)
}
companion object {
val TAG: String = AdsAdapter2::class.java.simpleName
const val ITEMS_PER_AD = 3
const val AD_FREQUENCY = ITEMS_PER_AD + 1
const val AD_VIEWTYPE = 0
const val CARD_VIEWTYPE = 1
}
override fun onMultipleItemAction(menuItem: MenuItem, selection: List<adslist>) {
TODO("Not yet implemented")
}
override fun getPopupText(position: Int): String {
TODO("Not yet implemented")
}
override fun getName(model: adslist): String? {
TODO("Not yet implemented")
}
private fun isCard(position: Int) = (position + 1) % AD_FREQUENCY != 0
private fun getCardForPosition(position: Int): adslist? {
val offset = position / AD_FREQUENCY
return if (isCard(position)) dataSet[position - offset] else null
}
override fun getItemViewType(position: Int) =
if (isCard(position)) CARD_VIEWTYPE else AD_VIEWTYPE
}
The logic of knowing which part of the list is gonna be an AD or a CARD is explained in the answer of the url above and it is applied on the last 3 functions and i do think the problem resides there but i cant see it clearly.
I'm not a super developer so it could be something so simple