//轮播图——方法 private void initlunbotu() { //数据源 List<String> list=new ArrayList<>(); list.add("http://www.chaziwang.com/shufa/k/113931.gif"); list.add("http://img3.imgtn.bdimg.com/it/u=1740425766,1378951113&fm=27&gp=0.jpg"); list.add("http://f.hiphotos.baidu.com/zhidao/wh%3D450%2C600/sign=4b5a52d4ae6eddc426b2bcff0ceb9acb/9c16fdfaaf51f3def121c55597eef01f3a29796b.jpg"); //添加适配器 viewPager.setAdapter(new MyAdapter_viewpager(getActivity(),list)); //handler延时 handler.sendEmptyMessageDelayed(0,1000); }
//图片刷新 必须要写否则图片加载太快 public void onPause() { super.onPause(); handler.removeCallbacksAndMessages(null); }
//适配器方法
public class MyAdapter_viewpager extends PagerAdapter { private final DisplayImageOptions options; private Context context; private List<String> list; public MyAdapter_viewpager(Context context, List<String> list) { this.context = context; this.list = list; options =new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(false)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式 .build(); } 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 imageView = new ImageView(context); //获取 ImageLoader.getInstance().displayImage(list.get(position%list.size()),imageView,options); //添加视图 container.addView(imageView); return imageView;//返回当前的视图对象 } @Override public void destroyItem(ViewGroup container, int position, Object object) { // container.removeView((View) object); } }