在网上看了一些资料,然后自己改成功了
一.重写viewpagerAdapter
public class MyViewPagerAdapter extends PagerAdapter{
private int len; //没什么用
private Context mcontext;
private ArrayList<View> mlist;
public MyViewPagerAdapter(ArrayList<View> mlist) { super(); this.mlist = mlist; this.len=mlist.size(); }
@Override public int getCount() { return Integer.MAX_VALUE; }
@Override public boolean isViewFromObject(View arg0, Object arg1) { return arg0==arg1; }
@Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView(mlist.get(position%mlist.size())); }
@Override public Object instantiateItem(ViewGroup container, int position) { ((ViewPager)container).addView(mlist.get(position%mlist.size()), 0); return mlist.get(position%mlist.size());
}
}
二.Activity写法 只写重点的几块
private ViewPager pager;
private MyViewPagerAdapter adapter;
private ArrayList<View> imgs;
private ImageView studyImage;
private String[] url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.study_layout);
initImageView();//加载图片
initViewPager();//加载viewpager
}
private void initViewPager() {
pager=(ViewPager)findViewById(R.id.study_viewPager);
adapter=new MyViewPagerAdapter(imgs);//别继承错了
pager.setAdapter(adapter);
pager.setOnPageChangeListener(this);//没什么用
pager.setCurrentItem(imgs.size()*100);//这是重点
}
//若图片为本地图片将url类型换成int[]即可(此处我用universal-imageloader加载图片)
private void initImageView() {
imgs=new ArrayList<View>();
url=new String[]{"http://images.china.cn/news/attachement/jpg/site3/20100420/8329526740659646910.jpg",
"http://img3.imgtn.bdimg.com/it/u=4025283544,1801047183&fm=23&gp=0.jpg",
"http://img2.xue163.com/pic28.nipic.com/20130418/4344061_132656658118_2.jpg",
"http://news.xinhuanet.com/photo/2014-09/28/127047283_14119070573481n.jpg"};
for(String s:url){
studyImage=new ImageView(this);
studyImage.setScaleType(ScaleType.FIT_XY);
imageLoader.displayImage(s,studyImage,options);
imgs.add(studyImage);
}
}