I am wondering why the view in first position is repeatedly called when I scroll the gridview even when I set the adapter. This causes my gridview cannot be scrolled smoothly as it repeatedly reload image.
It seems Picasso.with(context).load(file).into(viewImage);
causes the repeatedly called because if I delete this line, the gridview can be scrolled smoothly.
Any suggestions for improvement? Thanks.
Implementation for gridview array adapter
class HomeBrandCVC extends ArrayAdapter<Brand> {
private Context context;
private ArrayList<Brand> brandArrayList;
private Map<Integer, File> fileMap = new HashMap<>();
public HomeBrandCVC(Context c, ArrayList<Brand> brandArrayList) {
super(c, R.layout.homecvc, brandArrayList);
this.context = c;
this.brandArrayList = brandArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.homecvc, parent, false);
TextView nameLabel = (TextView) rowView.findViewById(R.id.homecvc_nameLabel);
nameLabel.setVisibility(View.INVISIBLE);
final Brand brand = brandArrayList.get(position);
nameLabel.setText(brand.name);
ImageView viewImage = (ImageView) rowView.findViewById(R.id.homecvc_viewImage);
Log.d("hellllo", String.valueOf(position));
if (fileMap.containsKey(position)) {
viewImage.setImageBitmap(BitmapFactory.decodeFile(fileMap.get(position).getAbsolutePath()));
} else {
viewImage.setImageResource(R.drawable.loadingimage);
new MyLeanCloudApp.GetImageFromDiskOrUrl(brand.imageFile, new MyLeanCloudApp.ImageHandlerGetImagesResponse() {
@Override
public void processFinish(File file) {
if (context != null) {
if (file != null) {
Picasso.with(context).load(file).into(viewImage);
fileMap.put(position, file);
}
}
}
}).execute();
}
return rowView;
}
}