Admob – 点击原生广告无效。

huangapple go评论67阅读模式
英文:

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, &quot;ca-app-pub-3940256099942544/2247696110&quot;)
                .forNativeAd { ad : NativeAd -&gt;
                    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(&quot;Ad Error: $adError&quot;)
                    }
                })
                .build()

            adLoader.loadAd(AdRequest.Builder().build())
        }
    }

My xml file
native_ad_view.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;com.google.android.gms.ads.nativead.NativeAdView
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_width=&quot;match_parent&quot;
    android:visibility=&quot;gone&quot;&gt;

    &lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@drawable/route_item_background&quot;
        android:clickable=&quot;true&quot;
        android:foreground=&quot;?android:attr/selectableItemBackground&quot;&gt;

        ...

    &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/com.google.android.gms.ads.nativead.NativeAdView&gt;

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

huangapple
  • 本文由 发表于 2023年6月8日 13:06:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定