在Recyclerview嵌入广告 [英] Embedding ads within Recyclerview

查看:1564
本文介绍了在Recyclerview嵌入广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表视图升级我的应用程序recyclerview。当我使用列表视图我不得不使用本教程中的列表视图中嵌入的广告:<一href="http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html">http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html

I am trying to upgrade my app from listview to recyclerview. When I was using listview I had embedded ads within the listview using this tutorial: http://googleadsdeveloper.blogspot.in/2012/03/embedding-admob-ads-within-listview-on.html

我不能够在recyclerview添加类似。这是如何的任何意见要在Recyclerview做?

I am not able to add it within recyclerview similarly. Any views on how this is to be done in a Recyclerview?

目前在我的列表视图的code是如下加载广告:

Currently in my listview the code is as below for loading ads:

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

这是它应该如何看:

推荐答案

在您的适配器,您首先需要覆盖ge​​tItemViewType,例如:

In your adapter, you first need to override getItemViewType, for example:

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

然后在onCreateViewHolder,根据类型抬高了不同的看法。事情是这样的:

Then in onCreateViewHolder, inflate a different view according to the type. Something like this:

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

这篇关于在Recyclerview嵌入广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆