Skip to content

Use Glide to Load Images from the web

rutura edited this page Apr 16, 2017 · 1 revision
  • MinSdk : 10 (Because of glide)
  • Glide takes care of the heavy lifting in loading images
  • et up a recycler view as you would normaly do. Previous apps have enough examples on that
  • The work is done in onBindViewHolder where you give the link to the image to be downloaded and the place holder where it will be placed.
    public void onBindViewHolder(ImageViewHolder holder, int position) {
            Glide.with(MainActivity.this)
                    .load(strArray[position])
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .centerCrop()
                    .placeholder(R.drawable.ic_cloud_download_black_24dp)
                    .error(R.drawable.ic_warning_black_24dp)
                    .crossFade()
                    .into(holder.image);
        }
  • Remember to clear that data of the image when it gets out of the view
   public void onViewRecycled(ImageViewHolder holder) {
            super.onViewRecycled(holder);
            Glide.clear(holder.image);
        }
  • This brings in great optimizations for example in killing connections downloading images that are out of the view.
  • To be good citizens, we also provide local resources to show if there has been an error and if the image is in the process of being downloaded:
   .error(R.drawable.ic_warning_black_24dp)
Clone this wiki locally