我有一个代码,其中的图像将显示在ViewPager上,图像将来自服务器。我正在使用Glide库,但是当我使用它时,如果服务器端没有图像,那么它默认会显示一个图像,但进度条会一直运行,直到我们返回或关闭应用程序。以下是我的代码,请检查一下。
public class SlidingImage_Adapter extends PagerAdapter {
private ArrayList<String> IMAGES;
private LayoutInflater layoutInflater;
private Context context;
ProgressBar progressBar;
private ImageLoader imageLoader;
SessionManager session;
public String string_gender;
public SlidingImage_Adapter(Context context, ArrayList<String> IMAGES) {
this.context = context;
this.IMAGES=IMAGES;
}
@Override
public Object instantiateItem(ViewGroup view, int position) {
// ImageView imageLayout = (ImageView) inflater.inflate(R.layout.screen_slide_fragment, view, false);
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.screen_slide_fragment, view, false);
ImageView imageView = (ImageView) item_view.findViewById(R.id.image);
// Session class instance
session = new SessionManager(context);
session.checkLogin();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
string_gender = user.get(SessionManager.KEY_GENDER);
ProgressBar spinner = (ProgressBar) item_view.findViewById(R.id.progressBar);
//This will help to load image and show progressBar
imageLoader = new ImageLoader(imageView, spinner, IMAGES.get(position));
//you need to add this line
view.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
@Override
public int getCount() {
return IMAGES.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState(){
return null;
}
public class ImageLoader{
public ImageLoader(ImageView imageView, final ProgressBar progressBar, String imagePath){
if(string_gender.equalsIgnoreCase("male"))
{
Glide.with(context)
.load(imagePath)
.placeholder(R.drawable.girl)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//handle error
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
}).into(imageView);
}
else{
Glide.with(context)
.load(imagePath)
.placeholder(R.drawable.boy)
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//handle error
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
}).into(imageView);
}
}
}
}
答案 0 :(得分:1)
在你的条件中添加这一行:
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
答案 1 :(得分:0)
在资源准备
中尝试此内容if (imageView.getDrawable().getConstantState() == getResources().getDrawable( R.drawable.girl).getConstantState())
progressBar.setVisibility(View.VISIBLE);
else
progressBar.setVisibility(View.GONE);