viewpager轮播图点的切换

来源:互联网 发布:windows下wget命令 编辑:程序博客网 时间:2024/06/16 02:47
<pre name="code" class="java">//处理点的切换private Handler mHandler = new Handler();//点切换的任务private class SwitchTask implements Runnable{@Overridepublic void run() {//执行的就是点的切换int currentItem = vp.getCurrentItem();if(currentItem == vp.getAdapter().getCount() - 1){currentItem = 0;}else{currentItem++;}vp.setCurrentItem(currentItem);mHandler.postDelayed(this, 3000);}}boolean isSwtich = false;//是否在切换private MyAdapter myAdapter;//开始切换public void startSwitch(){if(!isSwtich){mHandler.postDelayed(new SwitchTask(), 3000);isSwtich = true;}}//停止切换public void stopSwitch(){if(isSwtich){mHandler.removeCallbacksAndMessages(null);isSwtich = false;}}

然后设置imageview的触摸事件,iv.setOnTouchListener(this);回调onTouch()方法
<pre name="code" class="java">public boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:stopSwitch();break;case MotionEvent.ACTION_MOVE:startSwitch();break;case MotionEvent.ACTION_UP:startSwitch();break;default:break;}return true;//自己处理事件}



0 0
原创粉丝点击