我在我的android项目中使用Glide v4
。这是我使用listener
的实现。但这并没有给我任何回调:
Glide.with(HomeScreenActivity.this).load(url).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
e.printStackTrace();
videoCard_view.setVisibility(View.GONE);
video_linearlayout.setVisibility(View.GONE);
fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.e(TAG,"Video Image loaded");
videoCard_view.setVisibility(View.VISIBLE);
video_linearlayout.setVisibility(View.VISIBLE);
fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
return true;
}
}).apply(options.centerCrop()).into(video_ImageView);
但是当我使用下面的风格时它的工作完美。为什么会这样?:
Glide.with(HomeScreenActivity.this).load(url).apply(options.centerCrop()).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
Log.e(TAG,"video image loaded");
video_ImageView.setImageDrawable(resource);
}
});
答案 0 :(得分:1)
从false
方法返回onResourceReady
:
Glide.with(HomeScreenActivity.this).load(url).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
e.printStackTrace();
videoCard_view.setVisibility(View.GONE);
video_linearlayout.setVisibility(View.GONE);
fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.e(TAG,"Video Image loaded");
videoCard_view.setVisibility(View.VISIBLE);
video_linearlayout.setVisibility(View.VISIBLE);
fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
return false;
}
}).apply(options.centerCrop()).into(video_ImageView);
如果video_ImageView
或其父级为Invisible
或Gone
,则不会调用onLoadFailed
和onResourceReady
个回调,因为Glide
需要在进展到加载图像之前的大小,不可见的视图没有有效的大小。或者,您可以使用SimpleTarget
代替ImageView
:
Glide.with(HomeScreenActivity.this).load(url).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
e.printStackTrace();
videoCard_view.setVisibility(View.GONE);
video_linearlayout.setVisibility(View.GONE);
fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Log.e(TAG,"Video Image loaded");
videoCard_view.setVisibility(View.VISIBLE);
video_linearlayout.setVisibility(View.VISIBLE);
fetchSpecificQuoteContentType1(HomeScreenActivity.this, treatmentclient, "motivationalQuotes", strGender, motivations, quote_TextView, quote_ImageView, quotes_cardView);
return false;
}
}).apply(options.centerCrop())
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource,
@Nullable Transition<? super Drawable> transition) {
video_ImageView.setImageDrawable(resource);
}
});