Skip to content Skip to sidebar Skip to footer

Imageview Recyclerview Lazy Load

I have a RecyclerViewAdapter which in onBindViewHolder, I make an AsyncTask call to download the image required from Google Cloud Storage. There are some items in the RecyclerViewA

Solution 1:

Use Glide to display image in recyclerView

Add this dependencies to build.gradle

implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'

Inside onBindViewHolder

Request option to show error image, placeholder image etc. CircularProgressDrawable for placeholder drawable(optional)

val circularProgressDrawable = CircularProgressDrawable(mContext)
        circularProgressDrawable.strokeWidth = 5f
        circularProgressDrawable.centerRadius = 30f
        circularProgressDrawable.start()

        val requestOption : RequestOptions = RequestOptions()
                .placeholder(circularProgressDrawable)
                .error(R.drawable.ic_error_1)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.HIGH)
                .dontAnimate()
                .dontTransform()

initialize Glide as shown below

Glide.with(mContext)
            .load(model.imageUrl)
            .apply(requestOption)
            .into(holder.imageView!!)

Solution 2:

If it occured like this:

Failed to create image decoder with message 'unimplemented'

You catch the exception by judging the "bitmap == null?" Like the following codes: enter image description here

My English is not very well , I hope you can understand what I said!

Post a Comment for "Imageview Recyclerview Lazy Load"