我使用了android gridview,我动态地将项目添加到imageAdapter,但是显示了重复的图像项目。我使用ViewHolder处理每个网格。 这是我的getView代码:
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_product_image, null);
viewHolder = new ViewHolder();
viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
viewHolder.iv_image.setId(position);
viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
viewHolder.rl_grid = (RelativeLayout) convertView.findViewById(R.id.rl_grid);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if(mImages.get(position) instanceof ParseFile) {
int orientation = 0;
try {
orientation = Exif.getOrientation(((ParseFile)mImages.get(position)).getData());
}catch(ParseException pe){
Log.e("read Exif error", pe.getMessage());
}
Picasso.with(mContext)
.load(((ParseFile)mImages.get(position)).getUrl())
.placeholder(R.drawable.img_loading)
.fit()
.centerCrop()
.rotate(orientation)
.into(viewHolder.iv_image);
}else if(mImages.get(position) instanceof byte[]){
Bitmap bitmap = BitmapFactory.decodeByteArray((byte[])mImages.get(position), 0, ((byte[]) mImages.get(position)).length);
viewHolder.iv_image.setImageBitmap(bitmap);
}
return convertView;
}