英文:
Admob - Clicking on Native Ads does not work
问题
I'm trying to display ads in the Recycler view.
The ad itself is displayed correctly, but when I click on it, nothing happens.
My code for displaying ad:
class AdViewHolder(val binding: NativeAdViewBinding): RecyclerView.ViewHolder(binding.root) {
fun bind() {
val adLoader = AdLoader.Builder(binding.root.context, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd { ad : NativeAd ->
with(binding) {
imageView.setImageDrawable(ad.icon?.drawable)
titleTextView.text = ad.headline
ratingBar.rating = ad.starRating?.toFloat() ?: 0f
storeTextView.text = ad.store
actionButton.text = ad.callToAction
root.setNativeAd(ad)
root.visibility = View.VISIBLE
}
}
.withAdListener(object : AdListener() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Napier.d("Ad Error: $adError")
}
})
.build()
adLoader.loadAd(AdRequest.Builder().build())
}
}
My xml file native_ad_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:visibility="gone">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/route_item_background"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
I have tried to add method:
actionButton.setOnClickListener {
ad.performClick(Bundle())
}
But nothing changes.
英文:
I'm trying to display ads in the Recycler view.
The ad itself is displayed correctly, but when I click on it, nothing happens.
My code for displaying ad:
class AdViewHolder(val binding: NativeAdViewBinding): RecyclerView.ViewHolder(binding.root) {
fun bind() {
val adLoader = AdLoader.Builder(binding.root.context, "ca-app-pub-3940256099942544/2247696110")
.forNativeAd { ad : NativeAd ->
with(binding) {
imageView.setImageDrawable(ad.icon?.drawable)
titleTextView.text = ad.headline
ratingBar.rating = ad.starRating?.toFloat() ?: 0f
storeTextView.text = ad.store
actionButton.text = ad.callToAction
root.setNativeAd(ad)
root.visibility = View.VISIBLE
}
}
.withAdListener(object : AdListener() {
override fun onAdFailedToLoad(adError: LoadAdError) {
Napier.d("Ad Error: $adError")
}
})
.build()
adLoader.loadAd(AdRequest.Builder().build())
}
}
My xml file
native_ad_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:visibility="gone">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/route_item_background"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
I have tried to add method:
actionButton.setOnClickListener {
ad.performClick(Bundle())
}
But nothing changes
答案1
得分: 0
我解决了这个问题。
我需要添加这行代码:
(root as NativeAdView).callToActionView = root
英文:
I solved this problem.
I needed to add this line of code
(root as NativeAdView).callToActionView = root
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论