我遇到的问题是我ArrayIndexOutOfBound
photoViewAttacherList.get(position)
。现在我可以通过捕获错误轻松处理,但我无法理解为什么它发生在最初的位置。这是适配器的代码。这很奇怪,因为我在new PhotoViewAttacher
函数中添加了instantiate
,这将为每个添加的新项目调用。
public class FullImagePagerAdaptor extends PagerAdapter {
private final LayoutInflater inflater;
private final ArrayList<String> urls;
private final Context context;
private final ArrayList<String> descriptions;
private ArrayList<PhotoViewAttacher> photoViewAttacherList;
private TogglePagingListener togglePagingListener;
public FullImagePagerAdaptor(Context context, ArrayList<String> urls, ArrayList<String>
descriptions, TogglePagingListener togglePagingListener) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.urls = urls;
this.context = context;
this.descriptions = descriptions;
photoViewAttacherList = new ArrayList<>();
this.togglePagingListener = togglePagingListener;
}
@Override
public int getCount() {
return urls.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, final int position) {
View v = inflater.inflate(R.layout.fragment_full_image, container, false);
TextView description = (TextView) v.findViewById(R.id.txt_full_image_description);
ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.progress_bar_full_image);
ImageView imageView = (ImageView) v.findViewById(R.id.full_image_view);
if (descriptions != null && descriptions.get(position) != null) {
description.setText(descriptions.get(position));
}
Log.d("sg","Instantiating Pager:with position" + position);
ImageUtility.loadImage(context, urls.get(position),
GlobalVariables.IMAGE_TYPE.URL, 0, 0,
imageView, progressBar);
photoViewAttacherList.add(new PhotoViewAttacher(imageView));
// THIS IS WHERE ARRAYINDEXOUTOFBOUND EXCEPTION IS GETTING RAISED
photoViewAttacherList.get(position).setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
@Override
public void onMatrixChanged(RectF rect) {
if(isImageZoomed(position))
togglePagingListener.disablePaging();
else
togglePagingListener.enablePaging();
}
});
container.addView(v);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
photoViewAttacherList = null;
container.removeView((View) object);
}
public boolean isImageZoomed(int pos) {
return checkZoom(pos) != 1.0;
}
public float checkZoom(int pos){
return photoViewAttacherList.get(pos).getScale();
}
public interface TogglePagingListener {
public void enablePaging();
public void disablePaging();
}
答案 0 :(得分:0)
从您的代码中我了解一件事,您希望将新的PhotoViewAttacher
对象添加到现有列表中,并为该对象添加一个侦听器setOnMatrixChangeListener
。
为此,您将在列表中添加该对象,然后从列表中再次访问该对象,而不是您可以创建一个对象并添加到列表,并且可以添加事件侦听器到同一对象。这是一个干净的方法
还应重新计算position
以确切知道元素的插入位置,但这是final
,因此您无法对其进行更改,因此请为其声明一个新变量。
<强>段:强>
PhotoViewAttacher photoObj = new PhotoViewAttacher(imageView);
photoViewAttacherList.add(photoObj);
final int newPositionInserted = photoViewAttacherList.size() - 1; //Recalculating the position here to know exactly where the object was added
// Here you can use the object which was created so you do not need to worry about the index also
photoObj.setOnMatrixChangeListener(new PhotoViewAttacher.OnMatrixChangedListener() {
@Override
public void onMatrixChanged(RectF rect) {
if(isImageZoomed(newPositionInserted))
togglePagingListener.disablePaging();
else
togglePagingListener.enablePaging();
}
});