1,viewPagre的普通写法
public ImagePagerAdapter(Context context, List<Photo> imgList) {
this.mContext = context;
}
private void createImageViews() {
for (int i = 0; i < 4; i++) {
PhotoView imageView = new PhotoView(mContext);
imageView.enable();
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setMaxScale(2);
imageView.setImageResource(R.color.atlas_bgc);
viewList.add(imageView);
}
}
@Override
public int getCount() {
return mImgList == null ? 0 : mImgList.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
if(object instanceof PhotoView){
PhotoView view = (PhotoView)object;
view.enable();
view.setScaleType(ImageView.ScaleType.FIT_CENTER);
view.setMaxScale(2);
view.setImageResource(R.color.atlas_bgc);
viewList.add(view);
container.removeView(view);
}
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
final PhotoView currentView = viewList.remove(0);
final Photo image = mImgList.get(position);
container.addView(currentView);
if(!TextUtils.isEmpty(image.getPath())){
Glide.with(mContext).load(new File(image.getPath()))
.diskCacheStrategy(DiskCacheStrategy.NONE).into(currentView);
}
currentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onItemClick(position, image);
}
}
});
return currentView;
}