ListView with Image Adapter Locking the UI Thread - android

I have a listview that load images with a adapter. Every time that I scroll the screen, the listview calls the getView to refresh the listview, then the getView loads a image, doing that in my ui thread, locking my screen for a moment.
What I did was to load the images in a AsyncTask class. But the problem is that it take a little bit, and when you scroll you see the wrong image, after a while it louds the right image. I didn't want to have that problem, showing the wrong image for a moment.
I search and saw that there is no way to stop that listview automatic refresh.
What should I do to resolve that problem? Any tutorial would be helpful.

What should I do to resolve that problem?
Set the ImageView to show a placeholder image in getView(), while you fork the task to load the real image. Or, as others have suggested, use any number of libraries for managing all of that for you (and I echo the Picasso recommendation).

Related

Is it normal to use AsynTask in viewHolder in CustomAdapter

I have custom adapter (one image, 2 text fields), and I need to check image inside viewHolder. When image is already exist, show that image in ImageView and when image isn't exist, I need to check connection, if all good, show progressbar, download image, and show it, and if all bad then show default image. Sorry for my English.
No you should not implement a Async Task inside ViewHolder bcoz you do not know how much time it will take to load images and the user will be pissed at waiting for the listview to come up.
There are lot of tutorials out there you should use lazy loading for images if you are using URLS for images if there are not much images then you can store it in database or in a file and show any time in the listview
Hope it Helps :)
I've seen many people using, it is normal for small operations.
but it can't be used for tasks which take time - as the user experience is affected by lagging.
for small operations you can use it.

Using an adapter with asynchronous task

I have a class that extends BaseAdapter, which I use for a ListView. In the getView() method of that class I'm using an AsyncTask with a callback method to download an image (once downloaded, I store it, so I don't have to download it again). When the ListView loads the items first, only the first item displays an image an then starts to change the images (repeatedly displaying the images of the other items). Later also the other items start showing the same behaviour. After a while they stop cycling the images and each item shows the correct images. If I scroll the ListView the items are starting again to cycle the images.
This only happens, if I recycle the convertView parameter, that is passed to getView(). If I don't the images take very long to show up, and I'm afraid I'm creating too much new Views.
Thanks in advance for any advices, to get this working properly.
The simple way to get this going is to dump all your own image loading code and use something that already handles this, like Square's Picasso. There are plenty of alternatives if you do not like Picasso.
Otherwise, your tasks will need to be aware of the recycling, so they do not attempt to update an ImageView that now needs a different image. For example, you could use setTag() and getTag() on the ImageView to store (and retrieve) the URL the ImageView currently needs, and if the image downloaded by the task is some other URL, don't apply it.
But, again, please use an existing library for this. It's 2013: few people should be rolling their own download-the-images-for-use-in-ListView-rows code anymore.

Old items visible a while, in listview or gridview, when recycling

This is not a big issue, but it looks strange. In a list or gridview I load images with a delay (e.g. loaded from remote place). I'm recycling the items, using convertView. When I scroll down fast, I see the old images repeated, until the correct images are fetched and replace them.
Is there a way to change this? I tried, for example, at the very start of getView() (after initializing convertView, if necessary), to set the imageView visibility to GONE. And set to VISIBLE after the image has been fetched. But for some reason, this doesn't work (still looks the same).
Thanks!
Edit: I'm not even sure if this is normal behaviour when using recycled views, or if I'm doing something wrong. It doesn't look like a bug when the internet connection is fast, or when fetching from the file system. Then the new images are loaded very fast and the user doesn't see repeated items. But on a slow internet connection, it looks like a bug.
Before setting image in row just check if the image belong to the content of view.
I had the same problem sometime back and I had to change my image downloader so that it takes image url as well as meta data so that once download is complete, i can compare meta-data with view's data.

Android Gallery View and adapter getView gets called many time

I have a Gallery, which has an adapter connects to it. In the getView method, i have a custom layout, so that I can i have image and caption displayed together
The image is downloaded from an URL, which is done asynchously, and working as expected.
Currently i make each item to fill the screen, so i only have one item display at a time, basically i want to make it like a slide show.
let me be clear, currently i have an activity, and it only has one View, which is a Gallery.
problem occurs when I am swiping, the image bounces and stays at the same image. i need to swipe many times, hard, and long swipe, then i can get to next image.
i put a debug message in my custom adapter in the getView(), it seems getView is getting called many times (4 times), and position being passed is either the current position or the previous one, which explains why i am stuck at the same screen.
if i remove the remote downloading image part, or just use a static image form the phone, i don't have any more issues, in fact, the getView only gets called once, with correct position.
i am very frustrated, not sure what the problem is, could it be because i am downloading image asynchously, which will cause the image to update which causes getView to get called again to redraw itself?
i am not sure..
please help
Unfortunately this is a bug with gallery. Listviews will scroll nicely regardless of data being updated asynchronously. However, the gallery is just not coded up to par with the listview
When the gallery tries to update a visible view (due to your image loading callback) this view will "snap" back to the focused position. If you are changing the view in any way when it is scrolling it will snap. This is likely why you have to scroll hard to get away from the current view. It is trying to perform a callback on your view and only scrolling fast will prevent the callback from occurring before you move away from that view.
I've reported this bug a while ago here:
Android Issues
There are a few workarounds posted in there you can try if you are set on using a Gallery.
Unfortunately it hasn't gained attention from the Android developers.
It seems the issue is caused with views being set to "wrap_content" and the gallery having to remeasure/redraw its views
I have since migrated away from using the gallery and instead use a ViewPager. It is much easier to manage and you don't have to worry about this problem. This has been a known problem with the Gallery since the gallery was first introduced. I have no idea if this was fixed in any of the newer Android versions (3.x/4.x). As of 2.3.7 it is not fixed.
At first you should note: Any AdapterView (Gallery, ListView etc) doesn't guarantee that each time getView() method is called it will pass the same View instance parameter. It only guarantees that each view will have the same type (see Adapter.getIntemViewType() method docs)
So, when you start image downloading you should only specify position of the element. Then after the image has been downloaded you should bind specified ImageView with downloaded image in Adapter.getView() method call.
Take a look to the ImageDownloader from the Android samples here
The other approach is to use WeakHashMap in order to contain map of adapter views to its positions. I can provide you with code samples if you need.

Android ListView with an incrementally built ListAdapter

I am trying to figure out the right architecture for a list view that loads information from an http endpoint. My problem is that the information to be displayed has both text and image content. If I only load the text content the the UI is very responsive but if I also load the image content then it takes a few seconds for the list to be populated. I parse the content as it comes in and build up a list adapter but what I want to do is build up the list adapter incrementally so that the user sees the information as it becomes available instead of waiting until the last item is processed before any information is displayed. Currently I do everything with the help of AsyncTask so I'm aware of threads and how they can be helpful so I'd like to know how other people have worked around the issue of displaying list based information as quickly as possible.
So this solution doesn't incrementally build the ListView but if the text information comes quickly as you mentioned I think it might help:
Lazy load of images in ListView
Basically this solution will place a stub image in the ImageView until the desired image is finished downloading.
It is not only about loading images as far as i understand your question. Please look at "Implementing a Dynamically Loading Adapter (Endless List)" tutorial: http://codinglines.frankiv.me/post/14552677846/android-implementing-a-dynamically-loading-adapter

Categories

Resources