RecyclerView.Adapter, glide onLoadFailed` или `onResourceReady` не вызывается и изображение не отображается - PullRequest
0 голосов
/ 21 февраля 2020

Android studio 3.6

build.gradle:

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:

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

<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:

 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

, как вы можете видеть методы onLoadFailed или onResourceReady NOT CALLED. Заполнитель R.drawable.profile Успешное шоу.

1 Ответ

1 голос
/ 21 февраля 2020

Если у вас есть конфигурация, примените плагин kotlin-kapt, вы также должны заменить эти строки на gradle:

annotationProcessor 'com.github.bumptech.glide:compiler:[version]'

следует заменить на

kapt 'com.github.bumptech.glide:compiler:[version]'
...