Admob原生广告在RecyclerView中展示正常但点击无响应问题
RecyclerView中AdMob原生广告点击无响应的解决方案
你的问题主要出在两个地方:布局中子视图拦截了点击事件,以及没有正确将布局组件与NativeAdView关联。
问题分析
- 布局拦截点击:你在ConstraintLayout上设置了
android:clickable="true"和android:foreground="?android:attr/selectableItemBackground",这会让这个子布局优先消耗点击事件,导致NativeAdView无法接收到点击信号。 - 未关联广告组件:AdMob原生广告需要明确知道布局中的哪个视图对应广告的标题、按钮、图标等部分,否则无法正确处理点击跳转逻辑。你只设置了内容,但没有完成组件绑定。
解决方案
1. 修改布局文件
移除ConstraintLayout的clickable和foreground属性,避免拦截广告点击事件:
<?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"> <!-- 你的其他子视图保持不变 --> </androidx.constraintlayout.widget.ConstraintLayout> </com.google.android.gms.ads.nativead.NativeAdView>
2. 完善AdViewHolder的bind方法
在加载到NativeAd后,将布局中的视图与NativeAdView的对应属性绑定,让AdMob管理点击事件:
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) { // 绑定广告组件到NativeAdView root.headlineView = titleTextView root.iconView = imageView root.starRatingView = ratingBar root.storeView = storeTextView root.callToActionView = actionButton // 设置广告内容 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()) } }
3. 移除手动添加的点击监听
删掉你之前加的actionButton.setOnClickListener代码,因为AdMob会自动处理绑定后的组件点击事件,手动监听会干扰原有逻辑。
这样修改后,广告的点击事件就能被AdMob正确处理,跳转逻辑也会正常触发。
内容的提问来源于stack exchange,提问作者Alyona
相关产品推荐
相关产品推荐

