When adding banner the RecyclerView does not add a new position

1

Placing a banner in the recyclerView does not generate a new position for that element (banner) but instead "mounts" it or places it in the position of the elements practically eliminating the element that is in that position.

I'm using kotlin but if the solution is in java there is no problem, I need the idea basically how to do it, the code I have is the following:

OnCreate

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {

        val v: View = when (viewType) {

            TYPE_ADS -> LayoutInflater.from(parent.context).inflate(R.layout.card_ads, parent, false)
            else -> LayoutInflater.from(parent.context).inflate(R.layout.card_download, parent, false)
        }

        return ViewHolder(v)
    }

getItemViewType

override fun getItemViewType(position: Int): Int {

        return when {
            position % 8 == 0 -> TYPE_ADS
            else -> TYPE_CONTENT
        }
    }

ViewHolder

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindItems(item: files) {

            if (itemViewType == TYPE_CONTENT) {

                itemView.type_file.text = item.type
                itemView.img_file.setImageDrawable(item.img_file)
                itemView.name_file.text = item.name_file
                itemView.size_file.text = item.size_file
                itemView.path_file.text = item.path_file

                itemView.path_file.isSelected = true

            } else {

                val ads_view = itemView.layout_ads

                val adview = AdView(context)
                val adSize = AdSize(300, 70)
                adview.adSize = adSize
                adview.adUnitId = idBanner
                (ads_view as RelativeLayout).addView(adview)
                adview.loadAd(AdRequest.Builder().addTestDevice(idTestDevice).build())
            }
        }
    }
}

Thanks in advance for the collaboration.

    
asked by Leonardo Henao 02.10.2018 в 22:49
source

1 answer

0

I answer my own question in case someone needs a solution to this same inconvenience at some point.

Kotlin allows in the data class to define if one of its elements will be null

data class files(var type: String? = null, var img_file: Drawable? = null, var name_file: String? = null,
             var size_file: String? = null, var path_file: String? = null)

To all add the option that if necessary they were null.

At the moment of adding the elements I verify if the position where it is going to be added coincides with the itemViewType of the adapter

if (listFilesDownload.size % 8 == 0 && listFilesDownload.size != 0) {

    listFilesDownload.add(files(null, null, null, null))
    listFilesDownload.add(files(obtenerType(f), obtenerIcono(f), f.name, obtenerPeso(pesoF), f.path))

} else {
    listFilesDownload.add(files(obtenerType(f), obtenerIcono(f), f.name, obtenerPeso(pesoF), f.path))  }

Also add in the if the condition that if it is in position 0 add a normal element (it enters the else), if the condition is met first it will add an element with all its null variables to generate the position where the banner (TYPE_ADS) and then add the element.

    
answered by 03.10.2018 / 19:42
source