Set visibility of progress bar gone on completion of image loading using Glide library

前端 未结 12 1998
栀梦
栀梦 2020-12-04 11:04

Hi I want to have a progress bar for image which will shown while image loading but when image loading will be completed I want to set it to gone. Earlier I was using Picass

相关标签:
12条回答
  • 2020-12-04 11:25

    If you want to do this in KOTLIN, you can try that way:

        Glide.with(context)
                .load(url)
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        //TODO: something on exception
                    }
                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        Log.d(TAG, "OnResourceReady")
                        //do something when picture already loaded
                        return false
                    }
                })
                .into(imgView)
    
    0 讨论(0)
  • 2020-12-04 11:25

    Kotlin way

    Glide.with(context)
                    .load(image_url)
                    .listener(object : com.bumptech.glide.request.RequestListener<Drawable> {
                        override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Drawable>?,
                            isFirstResource: Boolean
                        ): Boolean {
                            return false
                        }
    
                        override fun onResourceReady(
                            resource: Drawable?,
                            model: Any?,
                            target: Target<Drawable>?,
                            dataSource: DataSource?,
                            isFirstResource: Boolean
                        ): Boolean {
                            img_product_banner.visibility = View.VISIBLE
                            return false
                        }
    
                    }).placeholder(R.drawable.placeholder)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(img_product_banner)
    
    0 讨论(0)
  • 2020-12-04 11:27

    In exception put a condition for show again the ProgressBar

     Glide.with(context)
        .load(image_url)
        .listener(new RequestListener<String, GlideDrawable>() {
            @Override
            public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                if(e instanceof UnknownHostException)
                    progressBar.setVisibility(View.VISIBLE);
                return false;
            }
    
            @Override
            public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .into(imageView);
    
    0 讨论(0)
  • 2020-12-04 11:32

    Update:

    Glide.with(this)
                .load(imageUrl)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable final GlideException e,
                                                final Object model, final Target<Drawable> target,
                                                final boolean isFirstResource) {
                        showProgress(false);
    
                        mNoContentTextView.setVisibility(View.VISIBLE);
    
                        return false;
                    }
    
                    @Override
                    public boolean onResourceReady(final Drawable resource, 
                                                   final Object model, 
                                                   final Target<Drawable> target, 
                                                   final DataSource dataSource, 
                                                   final boolean isFirstResource) {
                        showProgress(false);
    
                        mNoContentTextView.setVisibility(View.GONE);
                        mContentImageView.setImageDrawable(resource);
    
                        return false;
                    }
                })
                .into(mContentImageView);
    
    0 讨论(0)
  • 2020-12-04 11:33

    My answer was based on out-dated APIs. See here for the more up-to-date answer.

    0 讨论(0)
  • 2020-12-04 11:35

    In Kotlin you can do like below

    Glide.with(context)
                .setDefaultRequestOptions(RequestOptions().placeholder(R.drawable.ic_image_placeholder).error(R.drawable.ic_image_placeholder))
                .load(url)
                .listener(object : RequestListener<Drawable>{
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                        return false
                    }
    
                    override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        return false
                    }
    
                })
                .into(imageView)
    
    0 讨论(0)
提交回复
热议问题