kotlin - Android - Glide ".placeholder"方法无法识别

标签 kotlin android-glide

我有一个回收者的观点。在适配器的 onBindViewHolder方法我有以下代码来加载图像:

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)
    }

但是,Android Studio 说“占位符”是一个 Unresolved 引用。这令人困惑,因为 documentation表示这是加载占位符的正确方法。

我究竟做错了什么?

另外,这是我在 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.*

最后,这是我在 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'
}

最佳答案

如图 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.


所以,如果你想使用占位符,你有两个选择。

其中之一就是这样做:
Glide.with(context)
    .load(items[position])
    .apply(RequestOptions()
        .placeholder(R.drawable.animated_loading_icon)
    )
    .into(holder.imageView)

另一种选择是实现 Generated API

关于kotlin - Android - Glide ".placeholder"方法无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393663/

相关文章:

java - 为什么android在完成 Activity 后不清理内存?

android - 如何在gradle中强制下载aar版本的依赖项

Kotlin:自定义顺序的迭代器

gradle - 什么是 .kotlin_builtins 文件,我可以从我的 uberjars 中省略它们吗?

android - 条形码和二维码扫描不适用于CameraX/ZXing

Android Glide 错误 : cannot find symbol method crossFade()

android - 使用合并标记时,复合 View 不可见

gradle - 使用 Kotlin DSL 进行集成测试的单独 Gradle 源集

android - 使用 Glide 从 SD 卡加载图像

java - 从 Glide 下载大图像时出现内存不足错误