使用 Glide 加载占位符图像 - Android
Load placeholder images using Glide - Android
我有一个 ViewPager
,它使用自定义适配器。这些项目是带有 header 文本 (TextView title
) 的卡片,一些文本位于 header (TextView desc
) 下方,以及 ImageView
上方。图像通过 Glide 库从 DB 加载。它有效,图像加载成功,但在第一次加载图像时,卡片显示为空 - 即使没有文本。有没有办法在加载来自数据库的图像时在 ImageView
中显示带有文本和一些背景图像的卡片?
抱歉,如果我的问题看起来很愚蠢,我是第一次使用 Glide。
ViewPager
适配器代码:
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
// init root view (our 'card')
View view = layoutInflater.inflate(R.layout.vp_item, container, false);
// some code..
title.setText( events.get(position).getTitle() );
desc.setText( events.get(position).getDesc() );
// filling UI elements with data
Glide.with(context)
.load(events.get(position).getImageUrl())
.apply(new RequestOptions().override(800, 600))
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
// some code..
container.addView(view, 0);
return view;
}
您需要使用Glide提供的占位符功能如下:
Glide.with(context)
.load(events.get(position).getImageUrl())
.placeholder(R.drawable.placeholder_image) /// add this
.apply(new RequestOptions().override(800, 600))
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
我有一个 ViewPager
,它使用自定义适配器。这些项目是带有 header 文本 (TextView title
) 的卡片,一些文本位于 header (TextView desc
) 下方,以及 ImageView
上方。图像通过 Glide 库从 DB 加载。它有效,图像加载成功,但在第一次加载图像时,卡片显示为空 - 即使没有文本。有没有办法在加载来自数据库的图像时在 ImageView
中显示带有文本和一些背景图像的卡片?
抱歉,如果我的问题看起来很愚蠢,我是第一次使用 Glide。
ViewPager
适配器代码:
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
// init root view (our 'card')
View view = layoutInflater.inflate(R.layout.vp_item, container, false);
// some code..
title.setText( events.get(position).getTitle() );
desc.setText( events.get(position).getDesc() );
// filling UI elements with data
Glide.with(context)
.load(events.get(position).getImageUrl())
.apply(new RequestOptions().override(800, 600))
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);
// some code..
container.addView(view, 0);
return view;
}
您需要使用Glide提供的占位符功能如下:
Glide.with(context)
.load(events.get(position).getImageUrl())
.placeholder(R.drawable.placeholder_image) /// add this
.apply(new RequestOptions().override(800, 600))
.transition(DrawableTransitionOptions.withCrossFade())
.into(imageView);