I have an issue : in getView() function of ArrayAdapter, I execute loading image to ImageView with AsyncTask. But when this item is invisible(scroll listview), AsyncTask function still loading image. I want to disable AsyncTask process if item is invisible. Have a solution for this issues?
5 Answers
This would involve two steps.
1) Realize that the item view has scrolled out of the screen (or will no longer be used for another reason).
You can set use setRecyclerListener()
to add a RecyclerListener
object that will be notified whenever an item view is about to be recycled (onMovedToScrapHeap()
).
2) Actually abort the AsyncTask.
It depends on what you're actually doing inside. If doing an HTTP request (as it seems from the description) then calling cancel(true)
should cause an interrupt and abort the download.
However, before all this, consider: if the image was being downloaded and you interrupt this process, it means that it will probably have to be downloaded again when you scroll back up. Perhaps a better strategy would simply be to let the download complete, but not load the ImageView
itself it it has been reused (which you can detect by setting a tag on it).

- 54,791
- 16
- 125
- 154
You could check when the image is loaded if this is still the right ListView item where it should be displayed. As described in this answer, first store the path as the tag of the ImageView, and check in onPostExecute
:
protected void onPostExecute(Bitmap result) {
if (!imv.getTag().toString().equals(path)) {
/* The path is not same. This means that this
image view is handled by some other async task.
We don't do anything and return. */
return;
}
if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}
The bitmap will still be loaded fully though.

- 1
- 1

- 5,357
- 1
- 33
- 43
You can use cancel(true) method and check with isCancelled() if the task was cancelled.
Check the documentation http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
-
1`isCancelled()` would have been helpful only if the work was being done in a loop in the async task, so that `isCancelled()` can be executed repeatedly. I don't think image loading involves a loop. – faizal Jul 04 '14 at 15:40
If you do not use RecyclerViews, than you could also do the following in the getView()
if(vH.thumb.getTag() != null)
((LoadImage) vH.thumb.getTag()).cancel(true);
LoadImage loadImage = new LoadImage(vH.thumb, context.getActivity());
loadImage.execute(pic);
vH.thumb.setTag(loadImage);
Therefore you have to work with a ViewHolder(=vH). It's very usefull, if you are downloading Images, where you can cancel the download itself.
By asking isCancelled()
you now when to stop.
I know it's an old question, but that solution helped me a lot.

- 569
- 6
- 16
Use Universal Image Loader. In similar builds, i use
displayImage(URL, imageview);
function of this library in getView. Images get displayed on given imageview, and it's pretty quick. You can also reduce size of image for quicker load. Library will also save you from a lot of work and executing/ keeping track of a lot of asynctasks.

- 1,730
- 17
- 33
-
Dear Aksoy, I tried using Universal Image Loader but Task always running on foreground when i scroll listview – nguoitotkhomaisao Jul 04 '14 at 15:11