Viewpager实现首页图片广告轮播,早已经满大街都是。今天度娘了一下,可是我发现网上有一些只是单纯的图片轮播,而我想要做到用户点击或者拖拉的时候停止轮播,松开重新等待3秒(间隔的时间自定义),然后在继续轮播。看了一些帖子有的重写Viewpager也能实现,可是我觉得没必要这么复杂,自己捣鼓了一下。
下面直接上代码:
public class ViewpagerActivity extends AppCompatActivity {
private ViewPager viewPager;
private int index =0; //记录当前页数
private boolean mIsTouching =false ; //判断用户是否接触控件
private boolean mIsFirstLeave =false ; //判断是否刚离开控件
private int screewidth; //手机屏幕宽度
private int screehight; //手机屏幕高度
private Timer mTimer;
private TimerTask mTimerTask = null;
private ArrayList<String> ImgList = new ArrayList<>();
private View[] pageViews = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
//初始化图片地址
ImgList.add("http://b.hiphotos.baidu.com/zhidao/pic/item/0eb30f2442a7d9334907b645ac4bd11373f0015c.jpg");
ImgList.add("http://b.hiphotos.baidu.com/zhidao/pic/item/023b5bb5c9ea15ce3793816eb5003af33a87b253.jpg");
ImgList.add("http://t1.niutuku.com/960/17/17-434864.jpg");
ImgList.add("http://img.51ztzj.com/upload/image/20130424/201304231005_670x419.jpg");
ImgList.add("http://cdn.duitang.com/uploads/item/201202/07/20120207094418_YeReK.jpg");
/**
*这里主要是为了图片自适应宽高,你也可以直接在for循环中 new ImageView
*
**/
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
screewidth = wm.getDefaultDisplay().getWidth();
screehight = wm.getDefaultDisplay().getHeight();
pageViews = new View[ImgList.size()];
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < ImgList.size(); i++) {
pageViews[i] = li.inflate(R.layout.ad_item, null);
pageViews[i].setBackgroundDrawable(null);
ImageView imageView = (ImageView) pageViews[i].findViewById(R.id.ad_img);
ViewGroup.LayoutParams lp = imageView.getLayoutParams();
lp.width = screewidth;
lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
imageView.setLayoutParams(lp);
imageView.setMaxWidth(screewidth);
ImageLoader.getInstance().displayImage(ImgList.get(i),imageView);
}
viewPager = ((ViewPager) findViewById(R.id.vp));
viewPager.setOffscreenPageLimit(2); //设置预加载前后2页
viewPager.setAdapter(new AdvertisementAdapter(pageViews,this));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
index = viewPager.getCurrentItem() % pageViews.length; //记录当前位置
}
@Override
public void onPageScrollStateChanged(int state) {
switch (state) {
case ViewPager.SCROLL_STATE_DRAGGING: //拖拉的状态
mIsTouching = true;
mIsFirstLeave =true;
stopTimer(); //停止计时器
break;
case ViewPager.SCROLL_STATE_SETTLING: //已加载完毕
break;
case ViewPager.SCROLL_STATE_IDLE: //空闲闲的状态
mIsTouching = false;
if (mIsFirstLeave){
startTimer(); //如果手指刚离开控件开启计时器
}
break;
}
}
});
}
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
viewPager.setCurrentItem(index);
break;
}
}
};
private void startTimer(){
mIsFirstLeave =false;
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
if (index < (pageViews.length - 1)) {
index += 1;
} else {
index = 0;
}
mHandler.sendEmptyMessage(0); //这里用Handler主要是为了在主线程更新UI
}
};
mTimer.schedule(mTimerTask,3000, 3000);
//第一个参数,是 TimerTask 类
//第二个参数的意思是第一次执行的时间
//第三个参数的意思就是从第二次开始每隔多长的时间调用一次方法。
}
private void stopTimer(){
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
if (mTimerTask != null) {
mTimerTask.cancel();
mTimerTask = null;
}
}
}
下面是Adapter:
public class AdvertisementAdapter extends PagerAdapter {
private View[] pageViews;
private Context context;
public AdvertisementAdapter(View[] pageViews,Context context)
{
this.pageViews=pageViews;
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
if(pageViews!=null&&pageViews.length>0)
{
return pageViews.length;
}else
{
return 0;
}
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
public void destroyItem(View container, int position, Object object) {
if(pageViews!=null&&pageViews.length>0)
{
((ViewPager)container).removeView(pageViews[position % pageViews.length]);
}
}
/**
* 载入当前的position View
*/
@Override
public Object instantiateItem(View container, int position) {
if(pageViews!=null&&pageViews.length>0)
{
((ViewPager)container).removeView(pageViews[position % pageViews.length]);
((ViewPager)container).addView(pageViews[position % pageViews.length], 0);
return pageViews[position % pageViews.length];
}
else
{
return null;
}
}
}