在写轮播发现一个问题,往左滑动会出现IllegalStateException。告诉你只能有一个父亲。三思下,注销掉了destroyItem的通常写法在instantiateItem里addview之前判断下if(container.equals(view.getParent())) {
container.removeView(view);
},之后还注意到,@Override
public intgetItemPosition(Object object) {
returnPOSITION_NONE;
}。这两个方法一写,基本就解决了 一些问题,可是后来同事在写自动轮播的时候会出现空白页,打印后发现,从别的页面切换回来竟然会轮播线程执行两次,细想可能是轮播写的有点问题,就简单封装了一下。
public classMaxImagerViewPagerextendsViewPager {
private booleanisstart=false;
private longtime=2000;
privateListlist=newArrayList<>();
privateTimetasktimetask;
publicMaxImagerViewPager(Context context) {
this(context, null);
}
publicMaxImagerViewPager(Context context,AttributeSet attrs) {
super(context,attrs);
init();
}
private voidinit() {
timetask=newTimetask(this);
}
/**
*按下停止轮播, 如果离开的时间>= 2000毫秒,那么ViewPager继续轮播
*/
public voidstartlop() {
if(!isstart) {
isstart=true;
postDelayed(timetask,time);
}
}
public voidstoplop() {
isstart=false;
removeCallbacks(timetask);
}
@Override
protected voidonDetachedFromWindow() {
super.onDetachedFromWindow();
stoplop();
}
@Override
protected voidonAttachedToWindow() {
super.onAttachedToWindow();
startlop();
}
public static classTimetaskimplementsRunnable {
privateWeakReferenceweakReference;
publicTimetask(MaxImagerViewPager maxImagerViewPager) {
weakReference=newWeakReference(maxImagerViewPager);
}
@Override
public voidrun() {
MaxImagerViewPager maxImagerViewPager =weakReference.get();
maxImagerViewPager.setCurrentItem(maxImagerViewPager.getCurrentItem() +1
);
maxImagerViewPager.postDelayed(maxImagerViewPager.timetask,maxImagerViewPager.time);
}
}
@Override
protected voidonVisibilityChanged(View changedView, intvisibility) {
super.onVisibilityChanged(changedView,visibility);
if(VISIBLE== visibility) {
startlop();
}else if(INVISIBLE== visibility) {
stoplop();
}
}
@Override
public booleanonTouchEvent(MotionEvent ev) {
switch(ev.getAction()) {
caseMotionEvent.ACTION_DOWN:
//按下停止
stoplop();
break;
caseMotionEvent.ACTION_UP:
startlop();
break;
}
return super.onTouchEvent(ev);
}
private booleantimetag=false;
//监测两次时间的间隔
//提供方法设置viewpager的适配器
public voidsetMaXadapter(List list,Context context) {
this.list= list;
setAdapter(newMyadapter(context));
setCurrentItem(200);
}
public classMyadapterextendsPagerAdapter {
privateContextcontext;
publicMyadapter(Context context) {
this.context= context;
}
@Override
public intgetCount() {
returnInteger.MAX_VALUE;
}
@Override
public booleanisViewFromObject(View view,Object object) {
returnview == object;
}
@Override
publicObjectinstantiateItem(ViewGroup container, final intposition) {
final inti = position %list.size();
View view =list.get(i);
view.setOnClickListener(newOnClickListener() {
@Override
public voidonClick(View v) {
Toast.makeText(context,i+"",Toast.LENGTH_SHORT).show();
}
});
if(container.equals(view.getParent())) {
container.removeView(view);
}
Log.d("TAG","instantiateItem: ");
container.addView(view);
returnview;
}
@Override
public intgetItemPosition(Object object) {
returnPOSITION_NONE;
}
@Override
public voiddestroyItem(ViewGroup container, intposition,Object object) {
// container.removeView(list.get(position % list.size()));
}
@Override
public voiddestroyItem(View container, intposition,Object object) {
// super.destroyItem(container, position, object);
}
}
}