实际上,我希望在RecycleView上实施插页式Admob,以便当从列表中选择图像时,它会在进行下一个活动之前显示插页式广告?如何在适配器类上定义InterstitialAd? 有解决方案吗?
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();
}
});
}
答案 0 :(得分:0)
在上面的代码底部添加intertitialAd.load()
,然后在所有这些下面设置onClickListener:
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (interstitialAd.isLoaded){
interstitialAd.show();
}
}
});
答案 1 :(得分:0)
1-创建非页内广告:
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-添加广告事件以在关闭广告后移至下一个活动:
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
context.startActivity(new Intent(context, NextActivity.class));
}
});
在点击图像后,设置3-on On Click Listener以显示插页式广告:
images.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}else{
context.startActivity(new Intent(context, NextActivity.class));
}
}
});
最后,您的代码将如下所示:
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));
}
}
});
}
希望对您有帮助