我有以下代码,这对我很有用:
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);
但是我有很多图像因此我需要替换侦听器内部的代码。
GlideApp.with(this)
.load(url)
.listener(new RequestListener<Drawable>() { SOMETHING HERE
})
.into(imageView);
或者,如果您也可以通过以下方式:
GlideApp.with(this)
.load(url)
.listener(SOMETHING HERE)
.into(imageView);
我想用SOMETHING HERE替换所有这些代码,某些东西可以是变量或其他东西。
{
@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;
}
}
答案 0 :(得分:1)
你拥有的是一个匿名课程。它可以像任何其他变量一样分配,因此将其拉出并将其分配给一个
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;
}
};
然后你可以使用它
GlideApp.with(this)
.load(url)
.listener(listener)
.into(imageView);
您还可以定义一个完全独立的类
public class DrawableListener implements RequestListener<Drawable> {
private final ProgressBar progressBar;
// Add constructor
然后像
GlideApp.with(this)
.load(url)
.listener(new DrawableListener(progressBar))
.into(imageView);