[英]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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.