UnifiedNativeAdView 的 Admob 原生广告在 Kotlin 中不可点击

Admob native ads of UnifiedNativeAdView are not clickable in Kotlin

我遇到了一个关于 admob 原生广告的奇怪错误。一切正常,除了我在回调时找不到广告的 url 值,而且 UnifiedNativeAdView 不可点击,甚至在我使用 admob 模板时,里面的按钮也不会去任何地方。

这里是XML查看代码:

            <com.google.android.gms.ads.formats.UnifiedNativeAdView
               android:paddingTop="8dp"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:id="@+id/native_ad_view"
               android:background="#000000"
               android:elevation="20dp">

                 <LinearLayout
                    android:id="@+id/native_ad_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical">

                           <TextView
                             style="@style/AdAttribution"
                             android:id="@+id/native_ad_attribution"
                             android:visibility="gone"/>

                           <LinearLayout
                              android:id="@+id/native_ad_inside_layout"
                              android:layout_width="match_parent"
                              android:layout_height="0dp"
                              android:layout_gravity="center"
                              android:layout_weight="1"
                              android:orientation="horizontal"
                              android:visibility="gone">

                                  <ImageView
                                     android:id="@+id/ad_app_icon"
                                     android:layout_width="wrap_content"
                                     android:layout_height="wrap_content"
                                     android:layout_gravity="center"
                                     android:adjustViewBounds="true"
                                     android:maxWidth="85dp"
                                     android:scaleType="centerCrop" />

                                  <TextView
                                     android:id="@+id/ad_headline"
                                     android:layout_width="0dp"
                                     android:layout_height="wrap_content"
                                     android:layout_gravity="center"
                                     android:layout_weight="1"
                                     android:maxLines="3"
                                     android:paddingLeft="5dp"
                                     android:paddingRight="5dp"
                                     android:textSize="16sp"
                                     android:textStyle="bold" />

                                  <Button
                                     android:id="@+id/ad_call_to_action"
                                     android:layout_width="wrap_content"
                                     android:layout_height="wrap_content"
                                     android:layout_gravity="center"
                                     android:gravity="center"
                                     android:maxWidth="50dp"
                                     android:maxLines="3"
                                     android:textSize="12sp"
                                   />
                           </LinearLayout>

                  </LinearLayout>

            </com.google.android.gms.ads.formats.UnifiedNativeAdView>

Viewholder的代码:

    inner class TripViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

       val adView: UnifiedNativeAdView = view.native_ad_view as UnifiedNativeAdView
       var layoutView: LinearLayout = view.native_ad_layout
       var insideLayoutView: LinearLayout = view.native_ad_inside_layout
       var attributionView: TextView = view.native_ad_attribution

       var headlineView: TextView = view.ad_headline
       var callToActionView: Button = view.ad_call_to_action
       var iconView: ImageView = view.ad_app_icon
   }

onBindViewHolder代码:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {


    val item = mValues[position]

    if (item.nativeAd == null) {
        return
    }

    if (item.nativeAd!!.headline == null) {
        return
    }

    holder.attributionView.setVisibility(View.VISIBLE);
    holder.insideLayoutView.setVisibility(View.VISIBLE);
    holder.headlineView.text = item.nativeAd!!.headline

    if (item.nativeAd!!.callToAction == null) {
        holder.callToActionView.visibility = View.INVISIBLE
    } else {
        holder.callToActionView.visibility = View.VISIBLE
        (holder.callToActionView as Button).text = item.nativeAd!!.callToAction
    }

    if (item.nativeAd!!.icon == null) {
        holder.iconView.visibility = View.GONE
    } else {
        (holder.iconView as ImageView).setImageDrawable(item.nativeAd!!.icon.drawable)
        holder.iconView.visibility = View.VISIBLE
    }

    // Assign native ad object to the native view.
    holder.adView.setNativeAd(item.nativeAd!!)

}

这就是我调用 adLoader 的地方。我在后台使用 BroadcastReceiver:

调用它
      fun loadNativeAds(context: Context, listener: MyTripsContent.ContentEventsListener?) {
            if (!adList.isEmpty()) {
                return
            }
            try {
                var unitId: String? = ""
                lateinit var adLoader: AdLoader
                //test key
                unitId = "ca-app-pub-3940256099942544/2247696110"
                adLoader = AdLoader.Builder(context, unitId)
                        .forUnifiedNativeAd { ad: UnifiedNativeAd ->
                            // Show the ad.
                            adList.add(ad)
                            if (adList.size == 5) {
                                listener?.onLoadedDocuments(0)
                            }
                        }
                        .withAdListener(object : AdListener() {
                            override fun onAdFailedToLoad(errorCode: Int) {
                                // Handle the failure by logging, altering the UI, and so on.
                                if (!adLoader.isLoading) {
                                }
                            }

                            override fun onAdClicked() {
                                super.onAdClicked()
                            }
                        })
                        .withNativeAdOptions(
                                NativeAdOptions.Builder()
                                        // Methods in the NativeAdOptions.Builder class can be
                                        // used here to specify individual options settings.
                                        .build()
                        )
                        .build()
                adLoader.loadAds(AdRequest.Builder().build(), 5)
            } catch (exception: Exception) {
                exception.printStackTrace()
            }
        }

正如我提到的,广告效果很好,但它们不可点击。我不确定这是来自 admob 的错误,还是我的代码或我缺少的东西。我想以后其他开发者也会遇到同样的问题,解决掉就好了。 提前谢谢你。

所以由于缺乏详细的文档和示例,我发现我少了一行代码。许多其他示例也没有此行。我只在 onBindViewHolder

中添加了一行

来自这里:

if (item.nativeAd!!.callToAction == null) {
    holder.callToActionView.visibility = View.INVISIBLE
} else {
    holder.callToActionView.visibility = View.VISIBLE
    (holder.callToActionView as Button).text = item.nativeAd!!.callToAction
}

对此:

if (item.nativeAd!!.callToAction == null) {
    holder.callToActionView.visibility = View.INVISIBLE
} else {
    holder.callToActionView.visibility = View.VISIBLE
    (holder.callToActionView as Button).text = item.nativeAd!!.callToAction
    holder.adView.callToActionView = (holder.callToActionView as Button)
}

这意味着我需要将我的按钮附加为 callToActionView