public class MainActivity extends AppCompatActivity { int i; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); i++; int currentItem = vp.getCurrentItem(); vp.setCurrentItem(i); handler.sendEmptyMessageDelayed(1, 2000); } }; // String path="https://www.zhaoapi.cn/product/getCarts?uid=71"; String[] imager = {"https://img-my.csdn.net/uploads/201407/26/1406383242_9576.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383242_1721.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383219_5806.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383214_7794.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383213_4418.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383213_3557.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383210_8779.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383172_4577.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383166_3407.jpg", "https://img-my.csdn.net/uploads/201407/26/1406383166_2224.jpg" }; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); vp.setAdapter(new MyPagerAdapter()); new Thread(new Runnable() { @Override public void run() { handler.sendEmptyMessageDelayed(1, 2000); } }).start(); } class MyPagerAdapter 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) { ImageView img = new ImageView(MainActivity.this); ImageLoader.getInstance().displayImage(imager[position % imager.length], img); img.setScaleType(ImageView.ScaleType.FIT_XY); container.addView(img); return img; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
ViewPager+handler无限轮播
最新推荐文章于 2020-02-16 19:29:56 发布