简体   繁体   中英

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. 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. How can i accomplish that?

This is how my viewpager adapter looks:

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.

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. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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