简体   繁体   中英

Shorten progressBar code in android

I have the following code, which works well for me:

final ProgressBar progressBar = findViewById(R.id.progress);
progressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, 
PorterDuff.Mode.SRC_IN);

GlideApp.with(this)
        .load(url)
        .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }
        })
        .into(imageView);

But I have many many images therefore I need to replace the code that is inside the listener.

GlideApp.with(this)
        .load(url)
        .listener(new RequestListener<Drawable>() { SOMETHING HERE
        })
        .into(imageView);

Or if you can in the following way also:

GlideApp.with(this)
        .load(url)
        .listener(SOMETHING HERE)
        .into(imageView);

I want to replace all this code with SOMETHING HERE, that something can be a variable or something else.

{
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                progressBar.setVisibility(View.GONE);
                return false;
            }
        }

What you have is an anonymous class. It's assignable like any other variable, so pull it out and assign it to one

 RequestListener<Drawable> listener =  new RequestListener<>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }
    };

Then you can use it

GlideApp.with(this)
    .load(url)
    .listener(listener)
    .into(imageView);

You could also define an entirely separate class

public class DrawableListener implements RequestListener<Drawable> {
    private final ProgressBar progressBar;

    // Add constructor 

Then something like

GlideApp.with(this)
    .load(url)
    .listener(new DrawableListener(progressBar))
    .into(imageView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM