一、Handler
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==2){
pager.setCurrentItem(pager.getCurrentItem()+1);
handler.sendEmptyMessageDelayed(2,3000);
}
}
};
二、获取数据
pics = bean.getResult().getPics();
views = new ArrayList<ImageView>();
for (int i = 0; i<pics.size();i++){
ImageView imageView = new ImageView(Main2Activity.this);
ImageLoader.getInstance().displayImage(pics.get(i).getPic(),imageView);
views.add(imageView);
}
MyAdapte2 myAdapte2 = new MyAdapte2(views);
pager.setAdapter(myAdapte2);
三、适配器
public class MyAdapte2 extends PagerAdapter{
private List<ImageView> list;
public MyAdapte2(List<ImageView> list) {
this.list = list;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view==o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
ImageView imageView = list.get(position % list.size());
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((View) object);
}
}