gpt4 book ai didi

java - 缩短android中的进度条代码

转载 作者:行者123 更新时间:2023-12-02 11:29:14 24 4
gpt4 key购买 nike

我有以下代码,它对我来说效果很好:

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);

我想用这里的东西替换所有这些代码,这些东西可以是变量或其他东西。

{
@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;
}
}

最佳答案

你拥有的是一个匿名类。它像任何其他变量一样可以分配,因此将其取出并将其分配给一个

 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);

关于java - 缩短android中的进度条代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49397029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com