RecyclerView集成AdMob原生广告位置偏差、滑动末项触发IndexOutOfBoundsException
根因定位
首条广告错位出现在列表顶部
这个问题的本质是混合列表的类型判断和数据插入逻辑完全没对齐预设规则,踩了两个常见坑:
- 硬编码判定
position == 0为广告类型,没有按照「第7条Quote之后插入首条广告」的要求,把首条广告的起始位置设在混合列表索引7的位置 - 初始化广告列表的逻辑调用了
notifyItemInserted(0),直接把广告内容插到列表最开头,和Quote列表的排布顺序完全错位
列表滑到底部抛出IndexOutOfBoundsException
从堆栈报错Index:14, Size:3可以直接定位问题:调用mNativeAds.get(position)时传入的position值为14,但承载已加载原生广告的列表实际只有3个元素,有效索引范围是0-2,完全超出了列表边界。
对应的逻辑漏洞:
- 没有建立「RecyclerView适配器全局位置」和「两个独立列表(Quote列表、广告列表)真实索引」的映射关系,直接把适配器的原始position当作广告列表的索引用去取数据
- 类型判断逻辑失效,导致本应展示Quote的位置被误判为广告类型,触发了广告取数逻辑;就算是真的广告位置,也没有换算成广告列表的顺序索引,直接硬拿全局position查广告列表
- 总item数计算逻辑没有和实际已加载的广告数量做绑定,导致计算出来的可渲染item位置超过了广告列表的实际容量
修复方案
修正广告排布逻辑,解决首条广告位置错误
- 明确规则常量:首条广告的混合列表起始位置为7(对应前7个Quote占索引0-6的位置,之后依次按固定间隔插广告)
- 重写
getItemViewType方法,严格按照位置规则判断item类型,禁止硬编码顶部位置为广告:
private static final int AD_FIRST_POSITION = 7; // 首条广告在混合列表的位置 private static final int QUOTE_TYPE = 0; private static final int AD_TYPE = 1; @Override public int getItemViewType(int position) { // 前7个位置全部是Quote if (position < AD_FIRST_POSITION) { return QUOTE_TYPE; } // 从首条广告位置开始,每6个Quote插1个广告(也就是每7个位置为一个循环:1个广告+6个Quote) int offset = position - AD_FIRST_POSITION; if (offset % 7 == 0) { return AD_TYPE; } return QUOTE_TYPE; }
- 初始化列表数据时,严格按照上面的位置规则排布内容,不要主动在列表最开头插入广告
增加位置映射逻辑,解决越界崩溃
- 新增两个位置转换的辅助方法,分别计算混合列表位置对应的Quote真实索引、广告真实索引,禁止直接拿全局position查列表数据
- 绑定数据前先做类型校验和边界保护,避免非广告位置触发广告取数逻辑,同时防止广告未加载完成时的越界
@Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { int viewType = getItemViewType(position); if (viewType == QUOTE_TYPE) { // 换算Quote的真实列表索引 int quoteRealIndex = calculateQuoteIndex(position); if (quoteRealIndex < 0 || quoteRealIndex >= mQuoteList.size()) return; Quote targetQuote = mQuoteList.get(quoteRealIndex); // 绑定Quote视图 return; } if (viewType == AD_TYPE) { // 换算当前是第几个广告,对应广告列表的索引 int adRealIndex = calculateAdIndex(position); // 边界校验:如果广告列表还没加载到对应数量,直接跳过绑定 if (adRealIndex < 0 || adRealIndex >= mNativeAds.size()) return; UnifiedNativeAd targetAd = mNativeAds.get(adRealIndex); // 绑定广告视图 } } // 计算Quote在原列表的真实索引 private int calculateQuoteIndex(int adapterPosition) { int adCountBefore = calculateAdCountBeforePosition(adapterPosition); return adapterPosition - adCountBefore; } // 计算当前广告是列表里的第几个,对应广告列表的索引 private int calculateAdIndex(int adapterPosition) { return calculateAdCountBeforePosition(adapterPosition); } // 计算指定位置之前总共有多少个广告 private int calculateAdCountBeforePosition(int adapterPosition) { if (adapterPosition < AD_FIRST_POSITION) return 0; int offset = adapterPosition - AD_FIRST_POSITION; return offset / 7 + 1; }
- 重写
getItemCount方法,总item数为Quote总数量加上实际可用的广告数量(不超过已加载广告列表的大小),避免计算出不存在的item位置:
@Override public int getItemCount() { int totalQuote = mQuoteList.size(); // 按规则计算所有理论上需要的广告位数量 int totalAdSlots = 0; for (int pos = AD_FIRST_POSITION; pos < totalQuote + 10; pos += 7) { totalAdSlots++; } // 实际可用的广告位不能超过已加载的广告数量,也不能超过总广告位的需求 int realAdCount = Math.min(totalAdSlots, mNativeAds.size()); return totalQuote + realAdCount; }
内容的提问来源于stack exchange,提问作者John doe
相关产品推荐
相关产品推荐

