Hot questions for Using Glide in progress bar

Top 10 Java Open Source / Glide / progress bar

Question:

I have an application receives data (images) from json, I'm trying to show a Progressbar while image is loaded, but Progressbar gone before image appears, I want to make progressbar appears while image is loaded I'm using Glide library.

can I make the progress bar visible always behind the image view?

My adapter.java:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<Post> posts;
    private Context context;
    private RecyclerViewClickListener mClickListener;
    ProgressBar progressBar;
    public MyAdapter(List<Post> posts, Context context, RecyclerViewClickListener clickListener) {
        this.posts = posts;
        this.context = context;
        this.mClickListener = clickListener;
    }
    public List<Post> getItems(){
        return posts;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_item,
                parent, false);

        return new ViewHolder(v);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bindModel(posts.get(position));
    }
    @Override
    public int getItemCount() { return posts.size(); }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        Post mPost;
        ImageView image;
        ViewHolder(View v) {
            super(v);
            image = (ImageView) itemView.findViewById(R.id.cat_imageView);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress);
            progressBar.getIndeterminateDrawable().setColorFilter(Color.WHITE, PorterDuff.Mode.MULTIPLY);
            v.setOnClickListener(this);
        }
        void bindModel(Post post) {
            this.mPost = post;
            Glide
                    .with(context)
                    .load(post.getImgUrl())
                    .asBitmap()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.HIGH)
                    .listener(new RequestListener<String, Bitmap>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
                            return false;
                        }
                        @Override
                        public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            progressBar.setVisibility(View.GONE);
                            return false;
                        }
                    }).into(image);
        }

        @Override
        public void onClick(View view) {
            mClickListener.onClick(view, mPost);
        }
    }
}

Answer:

Use this may help you,

EDIT

Use Glide v-4.2.0 update gradle.build()

compile 'com.github.bumptech.glide:glide:4.2.0'

Then use below code this is working for me

  Glide.with(context)
            .load((post.getImgUrl()))
            .apply(new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .priority(Priority.HIGH))
            .listener(new RequestListener<Drawable>() {


        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }

        @Override
        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            progressBar.setVisibility(View.GONE);
            return false;
        }
    }).into(image);

see the image below

Question:

I am trying to learn DataBinding and stuck with one problem . I am not able to show Progress during loading the image with GLide . Below is my code .

 @BindingAdapter("android:src")
public static void loadImage(ImageView view, String url){
    if(!TextUtils.isEmpty(url))
    Glide.with(view).load(url).into(view);
}


   @BindingAdapter({"android:src","progressBar"})
public static void loadImage(ImageView view, String url, ProgressBar progressBar){
    if(!TextUtils.isEmpty(url))
        Glide.with(view).load(url)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }
                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        progressBar.setVisibility(View.GONE);
                        return false;
                    }
                })
                .into(view);
}

And i am calling it as :

 @Override
public Object instantiateItem(ViewGroup container, final int position) {
    LayoutInflater lp = LayoutInflater.from(container.getContext());
    ItemImageSliderBinding itemBinding = ItemImageSliderBinding.inflate(lp, container, false);
    container.addView(itemBinding.getRoot());
    itemBinding.setImageUrl(images.get(position));
    itemBinding.executePendingBindings();
    return itemBinding.getRoot();
}

As you can see there is two overloaded method loadImage But i want to use 2nd one to show progress . How can i do that . May be there is some other elegant way to do this. But i can not figure out what is it. Currently my layout looks like below.

<layout>
<data>
    <variable
        name="imageUrl"
        type="String" />
</data>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@{imageUrl}" />
    <ProgressBar
        android:id="@+id/progressImageSlider"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
</LinearLayout>

How can i bind ProgressBar with BindingAdapter to load images ?


Answer:

If you want using 2nd method:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:progressBar="@{progressImageSlider}"
        android:src="@{imageUrl}" />

But I think you should using placeholder in this case, please see my example

@BindingAdapter(value = { "android:src", "placeholder" }, requireAll = false)
public static void loadImage(ImageView view, String url, Drawable placeHolder) {
    if (!TextUtils.isEmpty(url)) {
        Glide.with(view.getContext()).load(url).placeholder(placeHolder).into(view);
    }
}

And call it in your xml

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:placeholder="@{@drawable/your_place_holder}"
        android:src="@{imageUrl}" />

hope this helps !

Question:

GIF image not visible again when its gone on button click event. It works for first time but not work on button click event;

I load GIF in imageview using below code;

Glide.with(getApplicationContext()).load(R.drawable.loading_progress).asGif().into(ImgV);

GIF gone code is below;

ImgV.setVisibility(View.GONE);

Again GIF is not visible on button click event using below code;

ImgV.setVisibility(View.VISIBLE);

Answer:

First Decision Load gif again with

Glide.with(getApplicationContext()).load(R.drawable.loading_progress).asGif().into(ImgV);

after that change visibility

ImgV.setVisibility(View.VISIBLE);

Second decision Just try to call

ImgV.invalidate()