viewpager的instantiateItem方法position怎么不从零开始
activityA中用到了viewpager,viewpager是动态操作view,跳到activityB后,再跳回来A,viewpager的instantiateItem方法的position就不从零开始了,导致数组越界。代码如下:
public class AdvertAdapter extends PagerAdapter {
private LinkedList<Advert> adverts;
private Context mContext;
private ArrayList<View> imagePageViews;
public HomeAdvertAdapter(Context mContext, LinkedList<Advert> adverts) {
this.adverts = adverts;
this.mContext = mContext;
imagePageViews = new ArrayList<View>();
}
@Override
public int getCount() {
return adverts.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView(imagePageViews.get(arg1));
}
@Override
public Object instantiateItem(View arg0, final int position) {
ImageView imageView = new ImageView(mContext);
imageView.setScaleType(ScaleType.FIT_XY);
imagePageViews.add((imageView));
((ViewPager) arg0).addView(imagePageViews.get(position));
return imagePageViews.get(position);
}
}
补充:移动开发 , Android