/*
* AUTHOR:Yolanda
*
* DESCRIPTION:create the File, and add the content.
*
* Copyright © ZhiMore. All Rights Reserved
*
*/
package com.yolanda.autoviewpager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
/**
* Created by Yolanda on 2016/5/3.
*
* @author Yolanda; QQ: 757699476
*/
public class AutoPlayViewPager extends ViewPager {
public AutoPlayViewPager(Context context) {
super(context);
}
public AutoPlayViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 播放时间
*/
private int showTime = 3 * 1000;
/**
* 滚动方向
*/
private Direction direction = Direction.LEFT;
/**
* 设置播放时间,默认3秒
*
* @param showTimeMillis 毫秒
*/
public void setShowTime(int showTimeMillis) {
this.showTime = showTime;
}
/**
* 设置滚动方向,默认向左滚动
*
* @param direction 方向
*/
public void setDirection(Direction direction) {
this.direction = direction;
}
/**
* 开始
*/
public void start() {
stop();
postDelayed(player, showTime);
}
/**
* 停止
*/
public void stop() {
removeCallbacks(player);
}
/**
* 播放上一个
*/
public void previous() {
if (direction == Direction.RIGHT) {
play(Direction.LEFT);
} else if (direction == Direction.LEFT) {
play(Direction.RIGHT);
}
}
/**
* 播放下一个
*/
public void next() {
play(direction);
}
/**
* 执行播放
*
* @param direction 播放方向
*/
private synchronized void play(Direction direction) {
PagerAdapter pagerAdapter = getAdapter();
if (pagerAdapter != null) {
int count = pagerAdapter.getCount();
int currentItem = getCurrentItem();
switch (direction) {
case LEFT:
currentItem++;
if (currentItem > count)
currentItem = 0;
break;
case RIGHT:
currentItem--;
if (currentItem < 0)
currentItem = count;
break;
}
setCurrentItem(currentItem);
}
start();
}
/**
* 播放器
*/
private Runnable player = new Runnable() {
@Override
public void run() {
play(direction);
}
};
public enum Direction {
/**
* 向左行动,播放的应该是右边的
*/
LEFT,
/**
* 向右行动,播放的应该是左边的
*/
RIGHT
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
addOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
if (state == SCROLL_STATE_IDLE)
start();
else if (state == SCROLL_STATE_DRAGGING)
stop();
}
});
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
ViewPager自动轮播广告
共28个文件
png:11个
xml:5个
gradle:3个
5星 · 超过95%的资源 需积分: 10 79 下载量 62 浏览量
2016-05-06
00:00:14
上传
评论 1
收藏 125KB ZIP 举报
温馨提示
ViewPager自动轮播广告、图片,解决滑动冲突,可以设置每个Item展示时间,可以设置轮播方向。原理讲解异步博客:http://blog.csdn.net/yanzhenjie1003/article/details/51327392
资源推荐
资源详情
资源评论
收起资源包目录
AutoViewPager.zip (28个子文件)
AutoViewPager
gradlew.bat 2KB
gradlew 5KB
gradle.properties 872B
app
src
main
res
mipmap-xxxhdpi
nohttp_trace.png 12KB
nohttp.png 10KB
nohttp_des.png 21KB
ic_launcher.png 10KB
nohttp_options.png 13KB
nohttp_put.png 10KB
nohttp_head.png 11KB
nohttp_patch.png 11KB
nohttp_get.png 10KB
nohttp_delete.png 10KB
nohttp_post.png 12KB
drawable
mipmap-hdpi
values
strings.xml 79B
styles.xml 155B
mipmap-xxhdpi
mipmap-mdpi
mipmap-xhdpi
layout
activity_main.xml 1KB
values-v21
styles.xml 159B
java
com
yolanda
autoviewpager
MainActivity.java 2KB
BannerAdapter.java 2KB
AutoPlayViewPager.java 4KB
AndroidManifest.xml 788B
app.iml 9KB
build.gradle 614B
libs
local.properties 476B
AutoViewPager.iml 944B
settings.gradle 16B
build.gradle 521B
共 28 条
- 1
资源评论
- zhangwenbin83872017-11-27不知道好不好用 我要找.net的,做微信开发,可惜你是JAVA的…
- lemonCase2018-02-28比较好用,作者好人
严振杰
- 粉丝: 4789
- 资源: 60
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功