在Recyclerview中embedded广告

我正在尝试从列表视图升级到recyclerview。 当我使用列表视图时,我使用本教程在列表视图中embedded了广告: http : //googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html

我无法在回收站中同样添加它。 任何关于如何在Recyclerview中完成的观点?

目前在我的列表视图中,代码如下载入广告:

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); } 

这应该是这样的:

在这里输入图像说明

更新:从谷歌提到这个video,它给出了完整的解释https://www.youtube.com/watch?v=LZCZSeFTvyk

Solutions Collecting From Web of "在Recyclerview中embedded广告"

在你的适配器中,你首先需要重写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; } 

在您的ArrayList中,将空值添加到您想展示广告的位置

  for(int i=0;i<arr.size();i++){ if(i%5==0) { alist.add(null); } alist.add(arr.get(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; } 

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

如何添加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; } }