在将 viewpager 与 picasso 库和 photoview 库一起使用时,我面临两个问题。
1)一些图像不缩放,而有时下一个图像被缩放。
2)有时当我缩放图像时,相邻图像也会与当前图像一起缩放。
如何解决以上2个问题?
public class ImageViewerPagerAdapter extends PagerAdapter implements PhotoViewAttacher.OnViewTapListener {
Context context;
private int count;
private ArrayList<String> images;
private PhotoViewAttacher mAttacher;
private boolean shouldFit = false;
public ImageViewerPagerAdapter(Context context, int count, ArrayList<String> images, boolean shouldFit) {
this.context = context;
this.count = count;
this.images = new ArrayList<>(images);
this.shouldFit = shouldFit;
}
@Override
public int getCount() {
return count;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((FrameLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
String imageUrl = images.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_image_viewpager_layout, container, false);
final PhotoView imageView = (PhotoView) view.findViewById(R.id.image);
if (shouldFit) {
Picasso.with(context).load(imageUrl).into(imageView, new Callback() {
@Override
public void onSuccess() {
if (mAttacher != null) {
mAttacher.update();
} else {
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnViewTapListener(ImageViewerPagerAdapter.this);
}
}
@Override
public void onError() {
}
});
} else {
Picasso.with(context).load(imageUrl).into(imageView, new Callback() {
@Override
public void onSuccess() {
if (mAttacher != null) {
mAttacher.update();
} else {
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnViewTapListener(ImageViewerPagerAdapter.this);
}
}
@Override
public void onError() {
}
});
}
container.addView(view);
return view;
}
@Override
public void onViewTap(View view, float v, float v1) {
Toaster.make(context,"hello");
((ImageViewPagerActivity) context).animateToolbar();
}
}