idanatz / OneAdapter

A Viewholderless Adapter for RecyclerView, who supports builtin diffing, states (paging, empty...), events (clicking, swiping...), and more.
MIT License
470 stars 45 forks source link

Multiple ViewType #40

Closed francescogatto closed 3 years ago

francescogatto commented 3 years ago

Hi!

I have this use case. Show admob native Ads in the recycler view. I can use multi modulo adapter, but how can i show Every 3 items of the modulo A, One item of the module B?

Thanks a lot

idanatz commented 3 years ago

Hi, It's simple, create 2 different modules: A for the regular item and B for Admob ad and attach them to the adapter. Create a new list of Diffable items - iterate over the regular items you want to display and insert an Ad model after every 3 regular items. Pass the list to the adapter to display.

You can look at the sample project and see MultipleItemModuleActivity for reference.

idanatz commented 3 years ago

did you manage to implement it? waiting for your feedback

francescogatto commented 3 years ago

I'm going to implement It soon

Il Lun 17 Mag 2021, 15:15 Idan Atsmon @.***> ha scritto:

did you manage to implement it? waiting for your feedback

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ironSource/OneAdapter/issues/40#issuecomment-842314803, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABAGC7B3ZKYT3PAONIB6FWDTOEJGLANCNFSM44NY3QTQ .

francescogatto commented 3 years ago

It works! You made a fantastic work with this library.. Thanks!

francescogatto commented 3 years ago

but i have another problem... this is my Admob module, `class AdMobModule : ItemModule() { init { var adLoader: AdLoader? = null var nativeAd: NativeAd? = null config {

        layoutResource = R.layout.item_admob
    }
    onBind { model, viewBinder, metadata ->
        AdLoader.Builder(context, "ca-app-pubXXXXXX").forNativeAd {
            nativeAd = it
            val styles = NativeTemplateStyle.Builder()
                .withCallToActionTypefaceColor(R.color.white)
                .withMainBackgroundColor(ColorDrawable(Color.WHITE)).build()
            viewBinder.findViewById<TemplateView>(R.id.adCard).isVisible = true
            viewBinder.findViewById<TemplateView>(R.id.my_template).setStyles(styles)
            viewBinder.findViewById<TemplateView>(R.id.my_template).setNativeAd(nativeAd)
        }.build().loadAd(AdRequest.Builder().build())

    }
    onUnbind { model, viewBinder, metadata ->
        nativeAd?.destroy()
    }
}

}`

but in onbind method is called every 1second.. so the admob view is refreshing fast and the user doesn't have the time to see it (and performance are very bad). i'm using the last library.

idanatz commented 3 years ago

Please add the rest of the code, how you create the item list, the rest of the definitions of the modules (the full ad module as well, its generics are missing), and the adapter creation. It sounds like your items list is full of ad models so you are getting onBind for each of them It's most likely a configuration issue since this library is covered with tests and I use it regularly.

francescogatto commented 3 years ago

I initialize the adapter in onviewcreated

oneAdapter = OneAdapter(recyclerDomande) { itemModules += DataModule() { } itemModules += AdMobModule() }

I set items in this way:

richiesteViewModel.getData()?.let {
                header?.isVisible = true
                val newList = mutableListOf<Diffable>()
                it.forEachIndexed { index, diffable ->
                    if(index%2==0)
                        newList.add(AdMobModel())
                    newList.add(it[index])
                }
                oneAdapter.setItems(newList)
            }

Here my Modules:

class DataModule(val callBAck: (String) -> Unit) : ItemModule<Data>() {
    init {
        config {
            layoutResource = R.layout.item_data
        }
        onBind { model, viewBinder, metadata ->
            val title = viewBinder.findViewById<TextView>(R.id.title)
            val status = viewBinder.findViewById<TextView>(R.id.status)
            title.text = "Pratica: ${model.protocollo}"
            status.isVisible = true
            status.text = "Stato: ${model.descStatoDomReadable}"

        }
        onUnbind { model, viewBinder, metadata ->
            // unbind logic like stop animation, release webview resources, etc.
        }
        eventHooks += ClickEventHook<Data>().apply {
            onClick { model, viewBinder, metadata ->
                callBAck(model.idProtocollo)
            }
        }
    }
}
class AdMobModule : ItemModule<AdMobModel>() {
    init {
        var adLoader: AdLoader? = null
        var nativeAd: NativeAd? = null
        config {
            layoutResource = R.layout.item_admob
        }
        onBind { model, viewBinder, metadata ->
           // if(adLoader == null) {
                adLoader = AdLoader.Builder(context, "ca-app-pub-").forNativeAd {
                    nativeAd = it
                    val styles = NativeTemplateStyle.Builder()
                        .withCallToActionTypefaceColor(R.color.white)
                        .withMainBackgroundColor(ColorDrawable(Color.WHITE)).build()
                    viewBinder.findViewById<TemplateView>(R.id.adCard).isVisible = true
                    viewBinder.findViewById<TemplateView>(R.id.my_template).setStyles(styles)
                    viewBinder.findViewById<TemplateView>(R.id.my_template).setNativeAd(nativeAd)
                }.build()
                adLoader?.loadAd(AdRequest.Builder().build())
           // }

        }
        onUnbind { model, viewBinder, metadata ->
            nativeAd?.destroy()
        }
    }
}

And my data:

class AdMobModel: Diffable {
    override val uniqueIdentifier: Long
        get() = Random.nextLong()

    override fun areContentTheSame(other: Any): Boolean {
       return true //true or false, it doesn't change nothing
    }
}
data class Data (

   omissis
): Diffable {
    val idProtocollo: String
        get() {
           omissis
        }

        override val uniqueIdentifier: Long
        get() = Random.nextLong()

    override fun areContentTheSame(other: Any): Boolean {
       return other is Data && idProtocollo == other.idProtocollo
    }

 }

I have the same problem with both modules. I tested that the viewmodel is called only one time. In the end, the action of native ads is not triggered. Everything works good with the Android Recyclerview adapter

idanatz commented 3 years ago

try this: in both models, instead of:

override val uniqueIdentifier: Long
        get() = Random.nextLong()

do:

override val uniqueIdentifier = Random.nextLong()

when creating a new id each time the adapter thinks its a new model and treats it in a different way.

Also, random is not that good due to the fact that there is a slim chance of duplicate id by 2 different models but that's not your issue.

francescogatto commented 3 years ago

It works!! I need to change the others modules :D

Thanks!