在我的应用中,我有一个带有三个标签的导航栏,每个标签都是一个片段,条形图如下所示:https://gyazo.com/5052f885effb3e0154d407b3bd8d3884它只是一个带有三个标签的普通导航栏。
所以现在我要显示我的非页内广告(我在我的MainActivity onCreate中加载),在这种情况下按第二个标签,选项卡名为" Ausraster"。
所以我做了以下操作:我在我的MainActivity onCreate中加载了非页内广告,如下所示:
public class MainActivity extends AppCompatActivity{
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId("MY ID");
mInterstitialAd.loadAd(adRequest);
}
然后我在MainActivity中编写了displayInterstitial方法:
public void displayInterstitial() {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
}
最后,我在第二个片段的onCreateView中写了这一行" Ausraster"片段:
((MainActivity)getActivity()).displayInterstitial();
现在应该实际折叠所有内容但问题如下:如果我启动应用程序,则在延迟3秒后显示非页内广告而不按第二个选项卡。但是如果我点击第二个标签,我希望它是唯一的显示。那我该怎么办?
我现在有一个多月的问题了,如果有人能告诉我为什么插页式广告没有按第二个标签就会显示,我会很高兴。
以下是我的所有代码,我希望你能解释一下,对不起我的英语不好:)
MainActivity:
public class MainActivity extends AppCompatActivity{
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
*Setup the NavigationView
*/
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId("MyId");
mInterstitialAd.loadAd(adRequest);
}
public void displayInterstitial() {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
}
}
TabFragment:
public class TabFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return x;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position)
{
if(position == 0){
return new KommentareFragment();
}
if(position == 1){
return new AusrasterFragment();
}
if(position == 2){
return new LustigesFragment();
}
return null;
}
@Override
public int getCount() {
return int_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position){
case 0 :
return "Kommentare";
case 1 :
return "Ausraster";
case 2 :
return "Lustiges";
}
return null;
}
}
final public void showInterstitial(){
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}
}
@Override
public void onAdClosed() {
}
});
}
}
最后我的第二个片段:
public class AusrasterFragment extends Fragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.ausraster_layout,container,false);
return rootView;
}
答案 0 :(得分:0)
ViewPager
预先加载其他屏幕外片段,以便在更改选项卡时使动画看起来更流畅。
因此,广告无需点击第二个标签即可加载。但是,您可以使用viewPager.setOffscreenPageLimit
更改要加载的屏幕外片段数,但其默认值和最小值为1.如果将其设置为1,则会提前创建第二个选项卡,但不会创建第三个选项卡。
要在选择第二个标签页时加载广告,您必须使用 ViewPager的pageChangeListener 并检查是否已选择第二个标签,然后展示广告。
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
if(position == 1)
displayInterstitial();
}
});
并且不要忘记从片段中删除((MainActivity)getActivity()).displayInterstitial();
。