我正在滑动显示服务器中的图像。
我想显示一个圆形进度条作为占位符。我从progress-bar-while-loading-image-using-glide尝试了此方法,但是它不起作用。
我想念什么?
我正在活动中显示recyclerview。当用户单击onItem时,图像在包含Viewpager的片段中打开。我正在使用滑行将图像加载到viewpager中。
转接器类别-
public class MyViewPagerAdapter extends PagerAdapter {
private ArrayList<Object> imagesList = new ArrayList<>();
public MyViewPagerAdapter(ArrayList<Object> images) {
this.imagesList = images;
}
@NonNull
@SuppressLint("ClickableViewAccessibility")
@Override
public Object instantiateItem(@NonNull final ViewGroup container, int position) {
Object recyclerViewItem = imagesList.get(position);
final View view = LayoutInflater.from(container.getContext()).inflate(R.layout.util_image_fullscreen_preview, container, false);
final TouchImageView imageViewPreview = view.findViewById(R.id.image_preview);
CircularProgressDrawable circularProgressDrawable =
new CircularProgressDrawable(container.getContext());
circularProgressDrawable.start();
Photos image = (Photos) imagesList.get(position);
GlideApp
.with(container.getContext())
.asBitmap()
.load(advertiser.getUrl())
.placeholder(circularProgressDrawable)
.fitCenter().diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageViewPreview);
container.addView(view);
return view;
}
@Override
public int getCount() {
return imagesList.size();
}
@Override
public boolean isViewFromObject(View view, Object obj) {
return view.equals(obj);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
最佳答案
尝试这种方式
在layout.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/progrss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
现在您加载图片的位置,
prgress = (ProgressBar)findViewById(R.id.progress);
progress.setVisibility(View.VISIBLE);
Glide.with(mContext)
.load(imageUrl)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
progress.setVisibility(View.GONE);
imageViewPriview.setImageDrawable(resource);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
progress.setVisibility(View.GONE);
}
});