Search code examples
androidandroid-progressbaruniversal-image-loader

Show single indeterminate progress bar for multiple images being loaded


So I have a ListView that has several ImageViews in each list item. I use the Universal Image Loader to load each image.

What I want is to show a single indeterminate progress bar spinner (say on the actionbar), while all the images in the list are loading, and when the last image has loaded the spinner is hidden.

I am not sure if this is possible, or the best technique for doing this?

Thanks.


Solution

  • Try this. In adapter:

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ...     
        ImageLoadingListener listener = new CombinedImageLoadingListener(3, progressBar);
        imageLoader.displayImage(uri1, imageView1, listener);
        imageLoader.displayImage(uri2, imageView2, listener);
        imageLoader.displayImage(uri3, imageView3, listener);
        ...
    }
    

    And this listener:

    class CombinedImageLoadingListener extends SimpleImageLoadingListener {
    
        private int imageCount;
        private ProgressBar progressBar;
    
        public CombinedImageLoadingListener(int imageCount, ProgressBar progressBar) {
            this.imageCount = imageCount;
            this.progressBar = progressBar;
        }
    
        @Override
        public void onLoadingComplete(Bitmap loadedImage) {
            imageCount--;
            if (imageCount == 0) {
                progressBar.setVisibility(View.GONE);
            }
        }
    }