Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Glide ".placeholder" method not recognized

I have a recycler view. In the adapter's onBindViewHolder method I have the following code to load an image:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Log.i("TEST-APP", "Binding View Holder")

        Glide.with(context)
                .load(items[position])
                .placeholder(R.drawable.animated_loading_icon)
                .into(holder.imageView)
    }

However, Android Studio is saying that "placeholder" is an unresolved reference. This is confusing because the documentation indicates that this is the correct way to load a placeholder.

What am I doing wrong?

Also, here are my imports in the RecyclerViewAdapter class

package com.example.myname.recylerviewtest

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.recyclerview_item_column.view.*

Lastly, here are my dependencies in build.gradle:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
api 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'me.zhanghai.android.materialprogressbar:library:1.4.2'
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
like image 458
Roymunson Avatar asked May 17 '18 14:05

Roymunson


1 Answers

As shown in the Glide documentation:

Most options in Glide can be applied using the RequestOptions class and the apply() method.

Use request options to apply (among others):

Placeholders
Transformations
Caching Strategies
Component specific options, like encode quality, or decode Bitmap configurations.

So, if you want to use placeholder, you have two options.

One of them is to do it this way:

Glide.with(context)
    .load(items[position])
    .apply(RequestOptions()
        .placeholder(R.drawable.animated_loading_icon)
    )
    .into(holder.imageView)

And the other option would be to implement the Generated API

like image 110
Mike Grimm Avatar answered Oct 20 '22 10:10

Mike Grimm