Giter Club home page Giter Club logo

Comments (9)

idanatz avatar idanatz commented on May 30, 2024

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.

from oneadapter.

idanatz avatar idanatz commented on May 30, 2024

did you manage to implement it?
waiting for your feedback

from oneadapter.

francescogatto avatar francescogatto commented on May 30, 2024

from oneadapter.

francescogatto avatar francescogatto commented on May 30, 2024

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

from oneadapter.

francescogatto avatar francescogatto commented on May 30, 2024

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.

from oneadapter.

idanatz avatar idanatz commented on May 30, 2024

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.

from oneadapter.

francescogatto avatar francescogatto commented on May 30, 2024

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

from oneadapter.

idanatz avatar idanatz commented on May 30, 2024

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.

from oneadapter.

francescogatto avatar francescogatto commented on May 30, 2024

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

Thanks!

from oneadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.