/** * 设置每个Item的播放时间,默认3000毫秒 */ public void setShowTime(int showTimeMillis); /** * 设置滚动方向,默认向左滚动 */ public void setDirection(Direction direction); /** * 开始自动滚动 */ public void start(); /** * 停止自动滚动 */ public void stop(); /** * 播放上一个 */ public void previous(); /** * 播放下一个 */ public void next();
public enum Direction {
/**
* 向左行动,播放的下一张应该是右边的
*/
LEFT,
/**
* 向右行动,播放的下一张应该是左边的
*/
RIGHT
}
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() {
// TODO 待实现开始播放
}
/**
* 停止
*/
public void stop() {
// TODO 待实现停止播放
}
/**
* 播放上一个
*/
public void previous() {
// TODO 待实现播放上一个
}
/**
* 播放下一个
*/
public void next() {
// TODO 待实现播放下一个
}
public enum Direction {
/**
* 向左行动,播放的应该是右边的
*/
LEFT,
/**
* 向右行动,播放的应该是左边的
*/
RIGHT
}
}
// 线程将被添加到消息队列,这个线程将会在UI线程(主线程)运行。 public boolean post(Runnable action); // 线程将被添加到消息队列,在指定的时间之后运行,这个线程将会在UI线程(主线程)运行。 public boolean postDelayed(Runnable action, long delayMillis);
/**
* 播放器
*/
private Runnable player = new Runnable() {
@Override
public void run() {
play(direction);
}
};
/**
* 开始
*/
public void start() {
postDelayed(player, showTime);
}
/**
* 停止
*/
public void stop() {
removeCallbacks(player);
}
/**
* 开始
*/
public void start() {
stop();
postDelayed(player, showTime);
}
/**
* 执行播放
*
* @param direction 播放方向
*/
private synchronized void play(Direction direction) {
// 拿到ViewPager的适配器
PagerAdapter pagerAdapter = getAdapter();
if (pagerAdapter != null) {// 判空
// Item数量
int count = pagerAdapter.getCount();
// ViewPager现在显示的第几个?
int currentItem = getCurrentItem();
switch (direction) {
case LEFT:// 如是向左滚动的,currentItem+1播放下一个
currentItem++;
// 如果+到最后一个了,就到第一个
if (currentItem >= count)
currentItem = 0;
break;
case RIGHT:// 如是向右滚动的,currentItem-1播放上一个
currentItem--;
// 如果-到低一个了,就到最后一个
if (currentItem < 0)
currentItem = count - 1;
break;
}
setCurrentItem(currentItem);// 播放根据方向计算出来的position的item
}
// 这就是当可以循环播放的重点,每次播放完成后,再次开启一个定时任务
start();
}
@Override
protected void 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();
}
});
}
package com.yolanda.autoviewpager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
/**
* <p>用ViewPager自定义的广告轮播器。</p>.
*
* @author Yolanda;
*/
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();
}
});
}
}
AutoPlayViewPager autoPlayViewPage = (AutoPlayViewPager) findViewById(R.id.view_pager); autoPlayViewPage.setAdapter(bannerAdapter); // 以下两个方法不是必须的,因为有默认值 autoPlayViewPage.setDirection(AutoPlayViewPager.Direction.LEFT);// 设置播放方向 autoPlayViewPage.setCurrentItem(200); // 设置每个Item展示的时间 autoPlayViewPage.start(); // 开始轮播
@Override
public int getCount() {
return resIds == null ? 0 : Integer.MAX_VALUE;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
position = position % resIds.size();
Object xxoo = resIds.get(position);
...
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有