我正在滑动显示服务器中的图像。 我想显示一个圆形进度条作为占位符。我从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);
}
}
答案 0 :(得分:1)
尝试这种方式
在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);
}
});
答案 1 :(得分:1)
尝试此解决方案
在您的xml
。file文件中添加进度栏
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="100dp"
android:visibility="visible" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="100dp" />
</RelativeLayout>
导入 gradle 以获取Glide
compile 'com.github.bumptech.glide:glide:3.7.0'
然后
final ProgressBar progressBar = (ProgressBar)
findViewById(R.id.progress);
progressBar.setVisibility(View.VISIBLE);
Glide.with(contexthere).load(advertiser.getUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable glideDrawable, GlideAnimation<? super GlideDrawable> glideAnimation) {
progressBar.setVisibility(View.GONE);
imageViewPreview.setImageDrawable(glideDrawable);
}
});
注意:-传递正确的上下文,即在片段类中使用getActivity()在活动类中使用ActivityName.this
答案 2 :(得分:1)
在视图寻呼机中,可以在实例化方法中使用以下方法。第一种方法是加载gif图像,第二种方法是使用进度条简单加载图像。
//// for loading gif images
RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
requestOptions.placeholder(R.drawable.itemes_bg);
requestOptions.error(R.drawable.itemes_bg);
Glide.with(context).applyDefaultRequestOptions(requestOptions).asGif().load(gifList.get(position)).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
Toast.makeText(context, "No Internet Connection...", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
}).into(holder.imageView);
//// for loading simple images
RequestOptions requestOptions = new RequestOptions();
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
requestOptions.placeholder(R.drawable.progress);
Glide.with(activity).applyDefaultRequestOptions(requestOptions).load(imageList.get(position).getImgUrl().trim()).listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
Toast.makeText(activity, "No Internet Connection", Toast.LENGTH_SHORT).show();
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);
请记住,您必须在xml中添加以下代码
<ProgressBar
android:id="@+id/progBar"
android:padding="150dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />