简体   繁体   English

如何在片段之间使用viewpager实施广告?

[英]How to implement ads with viewpager between fragments?

I have been looking for this answer a while, and i haven't found any good solution which would help me getting started in creating this. 我一直在寻找这个答案,而且我还没有找到任何好的解决方案来帮助我开始创建这个解决方案。 This is what i would like to do. 这就是我想做的。 Like we have view types in lists and we can show different layouts on different position in lists using those view types, i would like to do that also with viewpager. 就像我们在列表中有视图类型一样,我们可以使用这些视图类型在列表中的不同位置显示不同的布局,我也想使用viewpager做到这一点。 I'm showing some images from server in viewpager, so user can swipe them, but i would like to show user between those images on every 5 image ad. 我正在viewpager中显示服务器中的一些图像,因此用户可以滑动它们,但是我想在每5个图像广告中的这些图像之间显示用户。 How can i accomplish that? 我该怎么做?

This is how my viewpager adapter looks: 这是我的viewpager适配器的外观:

private class JokesImagesAdapter extends FragmentPagerAdapter {

    public JokesImagesAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        JokePageFragment fragment = new JokePageFragment();
        fragment.setArguments(jokes.get(position).toBundle());
        return fragment;
    }

    @Override
    public int getCount() {
        return jokes.size();
    }
}

And i guess that probably i need to create one more fragment and call it here with implementation of ads. 而且我想我可能还需要再创建一个片段,并在此处将其与广告的实现一起调用。

What kind of ads you want to show in your app - Interstitial or Native ad? 您想在应用中展示哪种广告-插页式或原生广告?

If you want to just show an interstitial ad on every 5 swipes - just attach an OnPageChangeListener to your ViewPager and count the swipes. 如果您只想在每5次滑动中展示插页式广告-只需在您的ViewPager上附加一个OnPageChangeListener并计算滑动数即可。

If you want to show a native ad, you have to create a new fragment (as you thought) which will be showing the ad. 如果要显示原生广告,则必须创建一个新片段(如您所想),该片段将显示广告。 You have to count and return the ad fragment in the getItem() method of your adapter. 您必须计算并返回适配器的getItem()方法中的广告片段。 Another way to handle this is to use one fragment which will be notified whether to show or not an ad view or a normal view. 解决此问题的另一种方法是使用一个片段,该片段将被通知是显示广告视图还是普通视图。

我认为您应该稍微修改笑话模型,该模型可以接受对象,而不仅仅是图像(和自定义参数),然后将对象转换为所需的数据类型,然后在填充笑话时,应在每个笑话中添加广告对象第五位,那就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM