public class MyPagerAdapter extends PagerAdapter {
Context context;
List<Adlist> listData;
Handler handler;
public MyPagerAdapter(Context context, List<Adlist> listData, Handler handler) {
this.context = context;
this.listData = listData;
this.handler = handler;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
//5 6 7 8
//0%4 = 0
//5%4 = 1
View view = View.inflate(context, R.layout.vp_item, null);
ImageView vp_iv = (ImageView) view.findViewById(R.id.vp_iv);
vp_iv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: //手指按下的事件
//全部取消
handler.removeCallbacksAndMessages(null);
break;
case MotionEvent.ACTION_UP: //手指抬起的事件
handler.sendEmptyMessageDelayed(1, 2000);
break;
case MotionEvent.ACTION_CANCEL: //取消的事件
handler.sendEmptyMessageDelayed(1, 2000);
break;
}
//true自己消费,false继续往下传
return true;
}
});
ImageLoader.getInstance().displayImage(listData.get(position%listData.size()).img, vp_iv);
container.addView(view);
return view;
}
}
无限轮播的适配器
最新推荐文章于 2021-05-31 22:04:39 发布