我创建了带有三个标签的导航栏。 我希望如果我点击第二个标签,会弹出插页式广告。
我已经在第二篇片段中尝试了这段代码:
public class SecondFragment extends Fragment {
private InterstitialAd mInterstitialAd;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.second_layout,container,false);
final AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("MYID");
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}
}
@Override
public void onAdClosed() {
}
});
return rootView;
}
但问题是,广告会在5秒后突然播放,这就是为什么我收到AdMob的警告邮件,我必须更改我的非页内广告。
那我怎么能直接弹出插页式广告呢?
答案 0 :(得分:0)
您所做的是在Ads本身上设置SetOnClickListener,而应该在viewpager2的表格布局上设置。
// Register page change callback here
viewPager.registerOnPageChangeCallback(onboardPageChangeCallback)
TabLayoutMediator(tabLayout, onboardViewPager) {
tab, position ->
}.attach()
和您的onboardPageChangeCallback
var onboardPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
if (position == 1) { // 0 index position
// Add you interstitial load code here
}
}
}