Comments (9)
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.
did you manage to implement it?
waiting for your feedback
from oneadapter.
from oneadapter.
It works! You made a fantastic work with this library.. Thanks!
from oneadapter.
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.
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.
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.
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.
It works!! I need to change the others modules :D
Thanks!
from oneadapter.
Related Issues (20)
- Support for StateRestorationPolicy HOT 4
- Scroll to first added element HOT 7
- Random crash in onCreate HOT 3
- use setItem, but the onBind method not be invoke HOT 12
- Question:Paging with Room Datasource HOT 2
- IndexOutOfBoundsException: Inconsistency detected HOT 2
- CalledFromWrongThreadException in 2.1.0 HOT 9
- Add drag & drop support
- Can we call next page with Paging Module to load without scrolling HOT 5
- Preselected Items HOT 1
- Load more animation when submit empty list HOT 1
- It not a function to get the data from adapter. HOT 1
- Retry button HOT 2
- View Binding with Kotlin 1.6.0 not working HOT 5
- How do i filter the list ?
- You can't use interface as M : Diffable in intem module HOT 1
- select all
- Dynamic layout manager
- Multiple Selection Cancellation Issue When Item Is Not Visible
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oneadapter.