How to Implement Interstitial Admob on RecycleView

断了今生、忘了曾经 提交于 2021-02-20 03:38:32

问题


Actually i am looking to implement interstitial Admob on RecycleView so that when an image from list is selected it show an Interstitial ad before moving to next activity?? How to define InterstitialAd on Adapter Class? Any Solution??

class imageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    ImageView images;
    RelativeLayout relativeLayout;
    Context mtcx;

    ArrayList<Images> img=new ArrayList<Images>();
    public imageViewHolder(@NonNull View itemView,Context mctx,ArrayList<Images> img ) {
        super(itemView);
        this.mtcx=mctx;
        this.img=img;
        images=(ImageView) itemView.findViewById(R.id.imageView);
        itemView.setOnClickListener(this);
       MobileAds.initialize(mctx, "ca-app-pub-3940256099942544~3347511713");
        interstitialAd = new InterstitialAd(mctx);

        interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                intent();
            }
        });         
    }

回答1:


1- Create an interstitial ad:

 MobileAds.initialize(context, "ca-app-pub-3940256099942544~3347511713");
            final InterstitialAd mInterstitialAd = new InterstitialAd(context);
            mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
            mInterstitialAd.loadAd(new AdRequest.Builder().build());

2- add Ad events for moving to next activity after close ad:

mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    context.startActivity(new Intent(context, NextActivity.class));
                }
            });

3- set On Click Listener for show Interstitial Ad after image click:

images.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mInterstitialAd.isLoaded()){
                        mInterstitialAd.show();
                    }else{
                        context.startActivity(new Intent(context, NextActivity.class));
                    }
                }
            });

finally your code will be like:

class imageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    ImageView images;
    RelativeLayout relativeLayout;
    Context mtcx;

    ArrayList<Images> img=new ArrayList<Images>();
    public imageViewHolder(@NonNull View itemView,Context mctx,ArrayList<Images> img ) {
        super(itemView);
        this.mtcx=mctx;
        this.img=img;
        images = (ImageView) itemView.findViewById(R.id.imageView);

        MobileAds.initialize(context, "ca-app-pub-3940256099942544~3347511713");
        final InterstitialAd mInterstitialAd = new InterstitialAd(context);
        mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                context.startActivity(new Intent(context, NextActivity.class));
            }
        });
        images.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mInterstitialAd.isLoaded()){
                    mInterstitialAd.show();
                }else{
                    context.startActivity(new Intent(context, NextActivity.class));
                }
            }
        });
    }

hope this help you




回答2:


add intertitialAd.load() to the bottom of the code above then set the onClickListener like this below all of that:

    itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        if (interstitialAd.isLoaded){
            interstitialAd.show();
           }

        }
    });


来源:https://stackoverflow.com/questions/53784698/how-to-implement-interstitial-admob-on-recycleview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!