Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel AsyncTask execute from hidden item on getView() function in Android?

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?

like image 941
nguoitotkhomaisao Avatar asked Nov 11 '22 05:11

nguoitotkhomaisao


1 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).

like image 124
matiash Avatar answered Nov 14 '22 23:11

matiash