Как установить TextView с составной прорисовкой, которая представляет собой изображение URL, загруженное с помощью Glide, в Kotlin? - PullRequest
2 голосов
/ 25 сентября 2019

Вот мой попытанный код.

val imageView = ImageView(holder.itemView.context)

holder.itemView.textView.setCompoundDrawablesWithIntrinsicBounds(
        GlideApp.with(holder.itemView.context).load(URL).into(imageView), 0, 0, 0)

Я также не уверен насчет цели изображения Glide.У меня это установлено как .into(imageView).Поскольку я хочу добавить изображение с помощью TextView, ImageView не инициализирован для загрузки изображения URL.Я думал создать его программно с помощью

val imageView = ImageView(holder.itemView.context)

Ошибка, которую я вижу, состоит в том, что GlideApp.with(holder.itemView.context).load(URL).into(imageView) не определяется как отрисовка.

Ответы [ 2 ]

1 голос
/ 25 сентября 2019

Вы должны создать imageView в формате XML.и в вашем случае сделайте это

    Glide.with(applicationContext)
                       .load("https://LINK")
                       .listener(object : RequestListener<Drawable> {
                           override fun onLoadFailed(
                                   glideException: GlideException?,
                                   any: Any?,
                                   target: com.bumptech.glide.request.target.Target<Drawable>?,
                                   exception: Boolean
                           ): Boolean {

                               return false
                           }

                           override fun onResourceReady(
                                   drawable: Drawable?,
                                   any: Any?,
                                   target: com.bumptech.glide.request.target.Target<Drawable>?,
                                   dataSource: DataSource?,
                                   resources: Boolean
                           ): Boolean {
                               //check this on how to set drawable to textView
///5475892/programmno-ustanovit-levuy-otrisovku-v-textview
                               //set drawable to textView or do whatever you want with drawable

                               return false
                           }
                       })
                       .submit()
0 голосов
/ 25 сентября 2019

В Kotlin вы можете сделать как:

 Glide.with(context).load("url").apply(RequestOptions().fitCenter()).into(
        object : CustomTarget<Drawable>(50,50){
            override fun onLoadCleared(placeholder: Drawable?) {
                textView.setCompoundDrawablesWithIntrinsicBounds(placeholder, null, null, null)
            }

            override fun onResourceReady(
                resource: Drawable,
                transition: Transition<in Drawable>?
            ) {
                textView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null)
            }
        }
    )
...