如何在标签页布局中实施非页内广告?

时间:2018-09-26 01:52:27

标签: android android-tablayout interstitial

当我将一个标签移到另一个标签时,我需要实施有兴趣的广告。我使用以下代码使用该代码,但没有显示感兴趣的广告。我该怎么办?

import java.awt.event.KeyEvent;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import javax.swing.*;

@SuppressWarnings("serial")
public class ProgressFun extends JPanel {
    private JProgressBar progressBar = new JProgressBar(0, 100);

    public ProgressFun() {
        progressBar.setStringPainted(true);
        final JButton startProgress = new JButton("Start Progress");
        startProgress.setMnemonic(KeyEvent.VK_S);
        startProgress.addActionListener(l -> {
            startProgress.setEnabled(false);
            progressBar.setValue(0);
            final MyWorker myWorker = new MyWorker();
            myWorker.execute();

            myWorker.addPropertyChangeListener(pcEvent -> {
                if (pcEvent.getPropertyName().equals("progress")) {
                    int value = (int) pcEvent.getNewValue();
                    progressBar.setValue(value);
                } else if (pcEvent.getNewValue() == SwingWorker.StateValue.DONE) {
                    startProgress.setEnabled(true);
                    try {
                        myWorker.get();
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                }
            });
        });

        add(progressBar);
        add(startProgress);
    }

    private static void createAndShowGui() {
        ProgressFun mainPanel = new ProgressFun();

        JFrame frame = new JFrame("Progress Fun");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

class MyWorker extends SwingWorker<Void, Integer> {

    @Override
    protected Void doInBackground() throws Exception {
        int progress = 0;
        setProgress(progress);
        while (progress < 100) {
            progress += (int)(5 * Math.random());
            progress = Math.min(progress, 100);
            TimeUnit.MILLISECONDS.sleep((int) (500 * Math.random()));
            setProgress(progress);
        }
        return null;
    }
}

现在,当用户转到A到B片段,B到C片段,C到B片段,B到A片段和C到A片段时,我需要放置插页式广告。这只是一个测试目的,因此我需要在此处放置很多广告。

我的AFragment代码

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
        R.drawable.score,
        R.drawable.fix,
        R.drawable.tv
};

public static int int_items = 3 ;

private InterstitialAd mInterstitialAd;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);





        mInterstitialAd = new InterstitialAd(MainActivity.this);
        mInterstitialAd.setAdUnitId(Ad_code.Interstitial2);
        mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice("B167DCA4FD495BF41A2298FBC4894E56").build());


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }


}

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        public void onPageScrollStateChanged(int state) {}
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

        public void onPageSelected(int position) {
            if(position == 1)
                showInterstitial();
        }
    });

    adapter.addFrag(new ScoreFrag(), "A");

    adapter.addFrag(new FixtureFrag(), "B");

    adapter.addFrag(new TVFrag2(), "C");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {

        if(position == 0){

            return new AFragment();
        }
        if(position == 1){
            return new BFragment();
        }
        if(position == 2){
            return new CFragment();
        }

        return null;
    }

    @Override
    public int getCount() {
        return int_items;
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0 :
                return "A";
            case 1 :
                return "B";
            case 2 :
                return "C";
        }
        return mFragmentTitleList.get(position);
    }
}


final public void showInterstitial(){
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if(mInterstitialAd.isLoaded()){
                mInterstitialAd.show();
            }

        }

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

主要问题在哪里?我是初学者。所以有人可以给我解决方案吗?

2 个答案:

答案 0 :(得分:0)

在您的 onPageSelected 方法中,删除if(position == 1)

public void onPageSelected(int position) {

       //if(position == 1) ---> remove this

       showInterstitial();
}

答案 1 :(得分:0)

FormBuilderDropdown