Search code examples
androidlistviewimageviewinvalidation

checking if android list item (Drawable) is still visible


Let's assume i got a list of ImageView and TextView with LinearLayout. Now i'm using simple ArrayAdapter extension that gets Strig[][] as items which is an array of double strings (each entry contains String[2] where the first string is a uri to the image and the second one is the text. i override getView to display the image and text , recyling views, of course :-). However since image loading, even from resources may be slow, i'm doing it in an async task, that means that i have a default image loaded and i use: startLoading (it's my method) that stakes the ImageView and the Uri and once the image is ready it places it in the image view.

However sometimes, since it's a list if the image is not loaded yet and I'm in the middle of the process it's worth while stopping the loading, and that's in case that the line i'm loading image for is no longer visible.

Since the views are recycled i can't check if ImageView is visible since it may be visible but with a different Drawable... is there a way for me to know at some point that my line in invalidated and not visible ? is there a simple way or shell i need to start using flags and tag pattern and stuff like that to discover that my line is no longer valid ?


Solution

  • What I do is based on how the ListView manages it's views.

    As there is a limited set of Views really used by the ListView and that might be passed to our Adapter through the convertView argument of the getView() method, I implemented the following:

    • I have a LinkedList class member where I store the Uris to be loaded. These Uris are consumed in an AsyncTask member to retrieve the pics.
    • Before returning the item view in getView(), my Adapter places the Urin of the item pic in the View.setTag(Object).
    • When I reuse a convertView, before reusing it's content I get the Uri it was previously linked to using View.getTag() and remove the corresponding element from the pending Uris LinkedList.