I took the approach of loading the images with an AsyncTask and attaching the task to the view in the adapter's getView function to keep track of which task is loading in which view. I use this in an app of mine and there's no scroll lag and all images are loaded in the proper position with no exceptions being thrown. Also, because the task does no work if it's canceled, you can perform a fling on your list and it should lag up at all.
The task:
public class DecodeTask extends AsyncTask<String, Void, Bitmap> {
private static int MaxTextureSize = 2048; /* True for most devices. */
public ImageView v;
public DecodeTask(ImageView iv) {
v = iv;
}
protected Bitmap doInBackground(String... params) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPurgeable = true;
opt.inPreferQualityOverSpeed = false;
opt.inSampleSize = 0;
Bitmap bitmap = null;
if(isCancelled()) {
return bitmap;
}
opt.inJustDecodeBounds = true;
do {
opt.inSampleSize++;
BitmapFactory.decodeFile(params[0], opt);
} while(opt.outHeight > MaxTextureSize || opt.outWidth > MaxTextureSize)
opt.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(params[0], opt);
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
if(v != null) {
v.setImageBitmap(result);
}
}
}
The adapter stores an ArrayList that contains the file paths of all the images that need loaded. The getView function looks like this:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iv = null;
if(convertView == null) {
convertView = getLayoutInflater().inflate(R.id.your_view, null); /* Inflate your view here */
iv = convertView.findViewById(R.id.your_image_view);
} else {
iv = convertView.findViewById(R.id.your_image_view);
DecodeTask task = (DecodeTask)iv.getTag(R.id.your_image_view);
if(task != null) {
task.cancel(true);
}
}
iv.setImageBitmap(null);
DecodeTask task = new DecodeTask(iv);
task.execute(getItem(position) /* File path to image */);
iv.setTag(R.id.your_image_view, task);
return convertView;
}
NOTE: Just a caveat here, this might still give you memory problems on versions 1.5 - 2.3 since they use a thread pool for AsyncTask. 3.0+ go back to the serial model by default for executing AsyncTasks which keeps it to one task running at a time, thus using less memory at any given time. So long as your images aren't too big though, you should be fine.
UPDATE: While this solution will still work, there have been great additions to the open source community for solving this problem in a cleaner way. Libraries like Glide or Picasso both handle loading items in a list quite well and I'd recommend you look into one of those solutions if possible.