ADmob原生广告没有使用recyclerview显示

时间:2017-10-10 14:50:46

标签: android android-recyclerview admob

我正在尝试将admob原生广告与recyclerview整合2天。它只显示一个空格。我记录的数据显示广告未能加载错误代码0.Facebook原生广告显示效果不错,但是admob没有显示。

这是我的适配器类。

public class ListViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // Declare Variables
    Context context;
    LayoutInflater inflater;
    ArrayList<HashMap<String, String>> data;
    HashMap<String, String> resultp = new HashMap<String, String>();
    private AdView mAdView;
    private static final int DEFAULT_VIEW_TYPE = 1;
    private static final int NATIVE_AD_VIEW_TYPEADMOB = 2;
    private NativeAd nativeAd;
    private NativeAdsManager manager;
    View facebooknativeview;
    View Admobnativeview;

    // Create native UI using the ad metadata.
    ImageView nativeAdIcon;
    TextView nativeAdTitle;
    MediaView nativeAdMedia;
    TextView nativeAdSocialContext;
    TextView nativeAdBody;
    Button nativeAdCallToAction;


    public ListViewAdapter(Context context,
                           ArrayList<HashMap<String, String>> arraylist) {
        this.context = context;
        data = arraylist;



    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {

            case NATIVE_AD_VIEW_TYPEADMOB:
                Admobnativeview = LayoutInflater.from(parent.getContext()).inflate(R.layout.admob_native_ad, parent, false);
                return new AdmobExpressNativeViewHolder(Admobnativeview);

            default:
                View MenuView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_item, parent, false);
                return new ArticleHolder(MenuView);


        }


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == DEFAULT_VIEW_TYPE) {

            ArticleHolder articleholder = (ArticleHolder) holder;
            resultp = data.get(position);
            articleholder.title.setText(resultp.get(PageFragment.TITLE));
            articleholder.link.setText(resultp.get(PageFragment.LINK));
            articleholder.summery.setText(resultp.get(PageFragment.SUMMERY));
            articleholder.time.setText(resultp.get(PageFragment.TIME));

            // Capture position and set results to the ImageView
            Picasso.with(context).load(resultp.get(PageFragment.IMAGE)).into(articleholder.image);
            Log.d("all texts", PageFragment.LINK + PageFragment.TITLE);
        } 
        else if(getItemViewType(position)==NATIVE_AD_VIEW_TYPEADMOB)
        {
            AdmobExpressNativeViewHolder adexpress=(AdmobExpressNativeViewHolder)holder;

            Log.d("admob native", "native starting");
            //Load the Ad
            AdRequest request = new AdRequest.Builder()
                    //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    //.addTestDevice("58DA103A6EB90C4260FD9202741F4EB4")
                    .build();
            adexpress.adView.loadAd(request);

        }
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(position != 0 && position % 2 == 0)
        {
            return NATIVE_AD_VIEW_TYPEADMOB;
        }
        return DEFAULT_VIEW_TYPE;
    }


    public class ArticleHolder extends RecyclerView.ViewHolder

    {
        TextView title;
        TextView link;
        TextView summery;
        TextView time;
        ImageView image;

        public ArticleHolder(final View itemView) {
            super(itemView);
            // Locate the TextViews in listview_item.xml
            title = (TextView) itemView.findViewById(R.id.title);
            link = (TextView) itemView.findViewById(R.id.link);
            summery = (TextView) itemView.findViewById(R.id.summery);
            time = (TextView) itemView.findViewById(R.id.time);

            // Locate the ImageView in listview_item.xml
            image = (ImageView) itemView.findViewById(R.id.image);

            itemView.setOnClickListener(new View.OnClickListener() {
                int position = getPosition();

                @Override
                public void onClick(View view) {

                    String stitle = title.getText().toString();
                    String ssummery = summery.getText().toString();
                    String stime = time.getText().toString();
                    String simage = resultp.get(PageFragment.IMAGE);
                    String slink = link.getText().toString();
                    Intent intent = new Intent(context, SingleItemView.class);
                    intent.putExtra("title", stitle);
                    intent.putExtra("summery", ssummery);
                    intent.putExtra("time", stime);
                    intent.putExtra("image", simage);
                    intent.putExtra("link", slink);
                    itemView.getContext().startActivity(intent);
                }
            });

        }

    }



    private class AdmobExpressNativeViewHolder extends RecyclerView.ViewHolder {
        NativeExpressAdView adView;
        public AdmobExpressNativeViewHolder(View admobnativeview) {
            super(admobnativeview);
            adView = (NativeExpressAdView)admobnativeview.findViewById(R.id.adView);
            adView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    Log.d("admob native","ad loaded");
                    notifyDataSetChanged();

                }
                @Override
                public void onAdOpened() {
                    super.onAdOpened();
                }
                @Override
                public void onAdLeftApplication() {
                    super.onAdLeftApplication();
                }
                @Override
                public void onAdFailedToLoad(int i) {
                    super.onAdFailedToLoad(i);
                    Log.d("admob native","ad failed to load"+i);

                }
                @Override
                public void onAdClosed() {
                    super.onAdClosed();

                }
            });



        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该查看Google Developers的this视频,该视频清楚地解释了有关 AdMob Native Express RecyclerView 的所有信息。

建议在MainActivity中加载广告。