在recyclerview android之间的横幅广告

时间:2018-04-02 05:35:58

标签: android google-admob

在我的应用程序中,我有一个recyclerview适配器,所以我想在项目之间显示横幅广告,我在我的适配器类中应用了以下代码,但它显示错误

  

无法加载广告,错误代码1

我检查了appid是否正确

public RecyclerViewAdapterMore.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerViewAdapterMore.ViewHolder viewHolder;

        if (viewType == AD_TYPE) {
            AdView adView = new AdView(context);
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(context.getString(R.string.app_id));

            float density = context.getResources().getDisplayMetrics().density;
            int height = Math.round(AdSize.BANNER.getHeight() * density);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, height);
            adView.setLayoutParams(params);

            AdRequest adRequest = new AdRequest.Builder().build();
            adView.loadAd(adRequest);

            viewHolder = new RecyclerViewAdapterMore.ViewHolder(adView);
        }
        else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false);
            viewHolder = new RecyclerViewAdapterMore.ViewHolder(view);
        }

        return viewHolder;
    }

0 个答案:

没有答案