I have a RecyclerView
and each item of RecyclerView
is having ImageView
. I am loading the image in that ImageView
using Glide
. I saw some blog says Glide
can load image asynchronously but I can not see this from the code below. It seems it only load one image when onBindViewHolder
is invoked. How does it show asynchrony?
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
private Context mContext;
private List<GalleryItem> mList;
public GalleryAdapter(Context mContext, List<GalleryItem> mList) {
this.mContext = mContext;
this.mList = mList;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public ViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.gallery_item);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_gallery,
parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
GalleryItem item = mList.get(position);
holder.mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Glide.with(mContext)
.load(item.getUrl())
.thumbnail(0.5f)
.into(holder.mImageView);
}
@Override
public int getItemCount() {
return mList.size();
}
public void addAll(List<GalleryItem> newList) {
mList.addAll(newList);
}
public void clear() {
mList.clear();
}
}
How does the Glide caching mechanism work? By default, Glide uses memory and disk caching to avoid unnecessary network calls, it checks into multiple layers of caches before initiating a new request call for an image.
The into(ImageView) method of Glide requires you to call it only on main thread, but when you pass the loading to a Timer it will be executed in a background thread. What you can do is to retrieve a bitmap by calling get() instead of into() and then set that bitmap on the ImageView by calling setImageBitmap() .
To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.
How Glide Cache Works. By default, Glide checks multiple layers of caches before starting a new request for an image: Active resources — Is this image displayed in another View right now? Memory cache — Was this image recently loaded and still in memory?
According to glide documentation here and here you should use the into() method that returns FutureTarget and not the one that returns a Target. This method take width and height, not an ImageView : "If you actually want to interact with a decoded image on a background thread, instead of using downloadOnly you can use the version of into() that returns a FutureTarget."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With