首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未调用RecyclerView.Adapter、glide onLoadFailed`或`onResourceReady`且未显示图像

未调用RecyclerView.Adapter、glide onLoadFailed`或`onResourceReady`且未显示图像
EN

Stack Overflow用户
提问于 2020-02-21 16:04:22
回答 1查看 515关注 0票数 2

Android studio 3.6

build.gradle:

代码语言:javascript
复制
android {

    dataBinding {
        enabled = true
    }
    // Configure only for each module that uses Java 8
    // language features (either in its source code or
    // through dependencies). E.g. lambda expressions.
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.project.client"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 80
        versionName "0.0.80"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }



    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    // use e.g. in my custom RecyclerView.Adapter
    //apply plugin: 'kotlin-android-extensions'
    apply plugin: 'kotlin-kapt'
    apply plugin: 'io.fabric'


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

下面是我的RecyclerView适配器:

代码语言:javascript
复制
class GazStationAdapter(newActivity: Activity, private val data: List<String>) :
    RecyclerView.Adapter<GazStationAdapter.ViewHolder>() {
    var activity: Activity? = null

    init {
        this.activity = newActivity
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflater = LayoutInflater.from(parent.context)
        val v = inflater.inflate(R.layout.gazstation_item_service_card, parent, false)
        return ViewHolder(v)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val serivceURL = "http://www.gravatar.com/avatar/11111?s=40x40&d=identicon"
        Debug.d("TAG", "onBindViewHolder: serivceURL = $serivceURL")
        Glide.with(holder.itemView.context)
            .load(serivceURL)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>?,
                    isFirstResource: Boolean
                ): Boolean {
                    Debug.d("TAG", "onBindViewHolder: glide_load_onLoadFailed")
                    return false;
                }

                override fun onResourceReady(
                    resource: Drawable?,
                    model: Any?,
                    target: Target<Drawable>?,
                    dataSource: DataSource?,
                    isFirstResource: Boolean
                ): Boolean {
                    Debug.d("TAG", "onBindViewHolder: glide_load_ready")
                    return false
                }

            })
            .placeholder(R.drawable.profile)
            .into(holder.image)
    }

    override fun getItemCount(): Int {
        return data.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val image: ImageView

        init {
            image = itemView.findViewById<View>(R.id.gazStaionServiceimage) as ImageView
        }
    }
}

这里是gazstation_item_service_card.xml

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center"
    android:layout_margin="4dp"
    android:orientation="vertical">


    <ImageView
        android:id="@+id/gazStaionServiceimage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

</LinearLayout>

但图像不能通过url加载。

这里是logcat:

代码语言:javascript
复制
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 W Glide   : Failed to find GeneratedAppGlideModule. You should include an annotationProcessor compile dependency on com.github.bumptech.glide:compiler in your application and a @GlideModule annotated AppGlideModule implementation or LibraryGlideModules will be silently ignored
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon
 D TAG     : onBindViewHolder: serivceURL = http://www.gravatar.com/avatar/11111?s=40x40&d=identicon

如您所见,onLoadFailedonResourceReady方法未被调用。占位符R.drawable.profile成功显示。

EN

回答 1

Stack Overflow用户

发布于 2020-02-21 16:41:37

如果你有配置应用插件kotlin-kapt,你还必须替换下面这一行到gradle:

代码语言:javascript
复制
annotationProcessor 'com.github.bumptech.glide:compiler:[version]'

应替换为

代码语言:javascript
复制
kapt 'com.github.bumptech.glide:compiler:[version]'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60334433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档