package com.zhongtou.bank.widget;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.provider.ContactsContract;
import android.support.v4.view.PagerAdapter
;import android.support.v4.view.ViewPager
;import android.util.AttributeSet;import android.view.MotionEvent;
import android.view.View
;import android.view.ViewGroup
;import android.view.animation.AccelerateInterpolator;
import com.zhongtou.bank.fragment.main.ChangeSpeedScroller;
import java.lang.reflect.Field;
import java.util.ArrayList;import java.util.Date
;import java.util.List;/
** * Created by IT on 2017/3/30. *//** *
无限轮播的viewpager */
public class MaxImagerViewPager extends ViewPager {
private boolean isstop = false;
private int loptime = 3500;
private Listlist = new ArrayList<>();
public MaxImagerViewPager(Context context) { this(context, null); }
public MaxImagerViewPager(Context context, AttributeSet attrs) { super(context, attrs); init(); }
private void init() { //设置viewpager滑动速度
Field mScroller = null; try {
mScroller = ViewPager.class.getDeclaredField("mScroller");
mScroller.setAccessible(true);
ChangeSpeedScroller scroller = new ChangeSpeedScroller(getContext(), new AccelerateInterpolator());
scroller.setDuration(200);
mScroller.set(this, scroller);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace(); }
new Thread() {
@Override
public void run() {
while (true) {
SystemClock.sleep(loptime);
if (isstop) {
//得到obtain的对象
Message message = Message.obtain();
message.what = 0;
message.arg1 = getCurrentItem() + 1;
handler.sendMessage(message);
}
}
}
}.start(); }
//设置轮播时间的方法
public void setLoptime(int time) {
this.loptime = time; }
//停止轮播 public void stoplop() {
if (isstop) {
isstop = false;
}
timetag = false;
}
//开始轮播
public void startlop() {
isstop = true;
}
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 0 && isstop) {
//允许轮播
int arg1 = msg.arg1;
setCurrentItem(arg1);
} else {
handler.removeMessages(0);
}
return true;
}
});
/** * 按下停止轮播, 如果离开的时间 >= 2000毫秒,那么ViewPager继续轮播 */
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
//按下停止 stoplop();
break;
case MotionEvent.ACTION_UP:
if (timetag == false) {
timetag = true; }
Timer(new Date().getTime());
break;
}
return super.onTouchEvent(ev);
}
private boolean timetag = false;
//监测两次时间的间隔
private void Timer(final long Curtime) {
new Thread() {
@Override
public void run() {
while (timetag) {
long time = new Date().getTime();
if (time - Curtime >= 2000) {
startlop();
}
}
}
}.start();
}
//提供方法设置viewpager的适配器 public void setMaXadapter(Listlist) {
this.list = list;
setAdapter(new Myadapter());
setCurrentItem(Integer.MAX_VALUE / 2);
}
class Myadapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
int i = position % list.size();
View view = list.get(i);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// super.destroyItem(container, position, object);
container.removeView(list.get(position % list.size()));
}
}
}