在Recyclerview中嵌入广告

我正在尝试将我的应用程序从listview升级到recyclerview。 当我使用listview时,我使用本教程在listview中嵌入了广告。

我无法在recyclerview中添加它。 有关如何在Recyclerview中完成此操作的任何观点?

目前在我的listview中,加载广告的代码如下:

if ((position % k) == 0) { if (convertView instanceof AdView) { return convertView; } else { // Create a new AdView AdView adView = new AdView(activity, AdSize.BANNER, ADMOB_ID); float density = activity.getResources().getDisplayMetrics().density; int height = Math.round(AdSize.BANNER.getHeight() * density); AbsListView.LayoutParams params = new AbsListView.LayoutParams( AbsListView.LayoutParams.FILL_PARENT, height); adView.setLayoutParams(params); adView.loadAd(new AdRequest()); return adView; } } else { return delegate.getView(position - (int) Math.ceil(position / k) - 1, convertView, parent); } 

它应该是这样的:

ListView项目

更新:从谷歌引用此video ,它给出了完整的解释

Solutions Collecting From Web of "在Recyclerview中嵌入广告"

在适配器中,首先需要覆盖getItemViewType,例如:

 @Override public int getItemViewType(int position) { if (position % 5 == 0) return AD_TYPE; return CONTENT_TYPE; } 

然后在onCreateViewHolder中,根据types膨胀不同的视图。 像这样的东西:

 @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { View v = null; if (viewType == AD_TYPE) { v = new AdView(activity, AdSize.BANNER, ADMOB_ID); float density = activity.getResources().getDisplayMetrics().density; int height = Math.round(AdSize.BANNER.getHeight() * density); AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.FILL_PARENT,height); v.setLayoutParams(params); v.loadAd(new AdRequest()); } else v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_layout, viewGroup, false); RecyclerView.ViewHolder viewHolder = new RecyclerView.ViewHolder(v); return viewHolder; } 

如何添加AD列表项不要跳过内容项不要将空项插入内容列表

 private static final int LIST_AD_DELTA = 3; private static final int CONTENT = 0; private static final int AD = 1; @Override public int getItemViewType(int position) { if (position > 0 && position % LIST_AD_DELTA == 0) { return AD; } return CONTENT; } @Override public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == CONTENT) { return new ContentRecyclerHolder(parent, layoutId) { @Override protected void onCardClick(CardView card) { fragmentManager.showPagerFragmentWithTransition(card.getContext(), getRealPosition(getAdapterPosition())); } }; } else { return new AdRecyclerHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_ad, parent, false)); } } @Override public int getItemCount() { int additionalContent = 0; if (data.size() > 0 && LIST_AD_DELTA > 0 && data.size() > LIST_AD_DELTA) { additionalContent = data.size() / LIST_AD_DELTA; } return data.size() + additionalContent; } @Override public void onBindViewHolder(BaseRecyclerHolder baseHolder, int position) { if (getItemViewType(position) == CONTENT) { ContentRecyclerHolder holder = (ContentRecyclerHolder) baseHolder; Content content = data.get(getRealPosition(position)); } else { AdRecyclerHolder holder = (AdRecyclerHolder) baseHolder; holder.adView.loadAd(); } } private int getRealPosition(int position) { if (LIST_AD_DELTA == 0) { return position; } else { return position - position / LIST_AD_DELTA; } } 

在ArrayList中,将空值添加到要显示广告的位置

  for(int i=0;i 

然后在getItemViewType()中,

  @Override public int getItemViewType(int position) { if(qlist.get(position)==null) return AD_TYPE; return CONTENT_TYPE; } 

然后,

 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = null; ViewHolder vh=null; if (viewType == AD_TYPE) { v=inflater.inflate(R.layout.adview_item, parent, false); vh=new AdviewHolder(v); } else { v = inflater.inflate(R.layout.cardview_items, parent, false); vh = new ContentViewHolder(v); } return vh; } 

这不会跳过任何项目,但会将广告插入到所需的位置。