2017-01-18 55 views
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(); 
} 


} 
+0

你爲什麼要共享一個PhotoViewAttacher?不應該每個PhotoView對象都應該有自己的PhotoViewAttacher? – JohnWowUs

回答

0

Android OS出現問題。當操作系統繪製視圖時,它會將其緩存在系統中,當系統請求相同的視圖時,操作系統將不會繪製新視圖,而是從緩存中檢索已經繪製的視圖。這就是爲什麼您可能會遇到諸如顯示在視圖中的下一圖像等問題。你也可以在列表中看到這樣的問題