为了账号安全,请及时绑定邮箱和手机立即绑定

ViewPager翻页嵌套广告轮播,是否为之烦恼呢???

标签:
Android

这里主要写广告轮播,布局文件就是一个简单viewpager
private Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 100) {
            mviewPager.setCurrentItem(currentItem);
        }
    }

};

private class SlideShowTask implements Runnable {

    @Override
    public void run() {
        synchronized (mviewPager) {
            currentItem = (currentItem + 1) % list.size();
            handler.sendEmptyMessage(100);
        }
    }
}

private void startPlay() {
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    scheduledExecutorService.scheduleAtFixedRate(new SlideShowTask(), 1, 3,
            TimeUnit.SECONDS);
}

//在onCreate方法:
if (isAutoPlay) {
startPlay();
}
initViews();

//然后创建initViews()方法
dotViewList = new ArrayList<ImageView>();
list = new ArrayList<ImageView>();

    for (int i = 0; i < 3; i++) {
        ImageView dotView = new ImageView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                new LayoutParams(LayoutParams.WRAP_CONTENT,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

        params.leftMargin = 10;
        params.rightMargin = 10;

        params.height = 20;
        params.width = 20;

        if (i == 0) {
            dotView.setBackgroundResource(R.drawable.banner_dian_focus);
        } else {

            dotView.setBackgroundResource(R.drawable.banner_dian_blur);
        }
        dotLayout.addView(dotView, params);

        dotViewList.add(dotView);
    }

    ImageView img1 = (ImageView) inflater.inflate(R.layout.scroll_vew_item,
            null);
    ImageView img2 = (ImageView) inflater.inflate(R.layout.scroll_vew_item,
            null);
    ImageView img3 = (ImageView) inflater.inflate(R.layout.scroll_vew_item,
            null);

    img1.setBackgroundResource(R.drawable.image_title1);
    img2.setBackgroundResource(R.drawable.image_title2);
    img3.setBackgroundResource(R.drawable.image_title1);

    list.add(img1);
    list.add(img2);
    list.add(img3);
    img1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
    img2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
    img3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
    ImagePaperAdapter adapter = new ImagePaperAdapter(
            (ArrayList<ImageView>) list);

    mviewPager.setAdapter(adapter);
    mviewPager.setCurrentItem(0);
    mviewPager.setOnPageChangeListener(new MyPageChangeListener());

//创建滑动监听implements OnPageChangeListener
boolean isAutoPlay = false;

    @Override
    public void onPageScrollStateChanged(int arg0) {
        switch (arg0) {
        case 1:
            isAutoPlay = false;
            break;
        case 2:
            isAutoPlay = true;
            break;
        case 0:

            if (mviewPager.getCurrentItem() == mviewPager.getAdapter()
                    .getCount() - 1 && !isAutoPlay) {
                mviewPager.setCurrentItem(0);
            } else if (mviewPager.getCurrentItem() == 0 && !isAutoPlay) {
                mviewPager.setCurrentItem(mviewPager.getAdapter()
                        .getCount() - 1);
            }
            break;
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageSelected(int pos) {
        currentItem = pos;
        for (int i = 0; i < dotViewList.size(); i++) {
            if (i == pos) {
                ((View) dotViewList.get(pos))
                        .setBackgroundResource(R.drawable.banner_dian_focus);
            } else {
                ((View) dotViewList.get(i))
                        .setBackgroundResource(R.drawable.banner_dian_blur);
            }
        }
    }
点击查看更多内容
13人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
4
获赞与收藏
155

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消