我使用PagerAdapter在Viewpager中显示一些图片。但令我困惑的是,' instantiateItem()'无法调用,这意味着无法显示图片。
以下是代码:
public class PictureScrollPagerAdapter extends PagerAdapter {
List<ArticleItme> list;
Context context;
DisplayImageOptions options;
ImageLoader imageLoader;
public PictureScrollPagerAdapter(List<ArticleItme> list, Context context) {
this.list = list;
this.context = context;
int screenWidth = ScreenSizeUtil.getScreenWidth(context);
options = new DisplayImageOptions.Builder()
.displayer(new MyDisPlay(screenWidth, screenWidth / 100 * 56))
.showImageForEmptyUri(R.drawable.def_image)
.showImageOnFail(R.drawable.def_image).cacheInMemory(true)
.cacheOnDisc(true).build();
imageLoader = ImageLoader.getInstance();
}
public List<ArticleItme> getList() {
return list;
}
public void setList(List<ArticleItme> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
final ArticleItme topshow = list.get(position);
ImageView image = new ImageView(context);
image.setScaleType(ImageView.ScaleType.FIT_XY);
image.setAdjustViewBounds(true);
/* image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (topshow.getModelid() == 10) {
Intent intent = new Intent(context, SpecialActivity2.class);
intent.putExtra("SpecialActivity", topshow);
context.startActivity(intent);
} else if (topshow.getModelid() == 2) {
Intent intent = new Intent(context, PhotosActivity.class);
intent.putExtra("SpecialActivity", topshow);
context.startActivity(intent);
} else if (topshow.getModelid() == 1) {
Intent intent = new Intent(context, NewsActivity.class);
intent.putExtra("SpecialActivity", topshow);
context.startActivity(intent);
}
}
});*/
imageLoader.displayImage(topshow.getThumb(), image, options);
((ViewPager) container).addView(image);
return image;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
class MyDisPlay implements BitmapDisplayer {
private int newWidth;
private int newHeight;
public MyDisPlay(int newWidth, int newHeight) {
this.newHeight = newHeight;
this.newWidth = newWidth;
}
@Override
public Bitmap display(Bitmap bitmap, ImageView imageView,
LoadedFrom loadedFrom) {
Bitmap scaleImg = BVBitmapUtil
.scaleImg(bitmap, newWidth, newHeight);
imageView.setImageBitmap(scaleImg);
return scaleImg;
}
}
}
我测试了可以调用&#39; getCount()&#39;但是&#39; instantiateItem&#39;始终无法被称为