Reputation: 2865
I am currently using a custom adapter to display an ImageView and two TextViews per row in a ListView.
Within the overridden getView for the adapter, I have this code for the ImageView:
final ImageView img = (ImageView) view.findViewById(R.id.rowImg);
new Thread(new Runnable() {
public void run() {
final BitmapDrawable b = downloadAvatar(urlToDownload);
img.post(new Runnable() {
public void run() {
img.setImageDrawable(b);
}
});
}
}).start();
The downloadAvatar method basically just uses AndroidHttpClient and HttpGet. The above method works, but my question is how do I optimize it? Scrolling is choppy; I know it's probably calling getView() and downloading the image each and every time it enters the viewable area. Any tips?
Upvotes: 0
Views: 385
Reputation: 72311
I would suggest to use the ignition-library
which has an component called RemoteImageView
, which will load asynchronously url images, and the library will also keep a cache of those images, so they are not downloaded every time you re-create your ListView
rows.
I use it, and I find it very usefull and robust.
Upvotes: 0
Reputation: 11807
Here's an option:
Inside getView
check if you have the current image downloaded. If it isn't downloaded yet - download it and store it in a directory of your choice. If it is present in that directory - just load it.
Depending on the image size and count you can also make some kind of cache to store the image objects required for the visible (and maybe the closest invisible above and below the visible) items.
Upvotes: 0
Reputation: 22291
Please see below Lazy Loading listview's source link and universal image loader example for that, it may help you.
Android Universal Image Loader
Upvotes: 1
Reputation: 22064
In your case, you'll need to do what's called "lazy load of images" which will cache those images once downloaded. Check this out: Lazy load of images in ListView
Upvotes: 0