0
當使用viewpager與picasso庫和photoview庫時,我遇到了2個問題。viewpager + picasso + photoview issue
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();
}
}
你爲什麼要共享一個PhotoViewAttacher?不應該每個PhotoView對象都應該有自己的PhotoViewAttacher? – JohnWowUs