Skip to content

Instantly share code, notes, and snippets.

@n8ebel
Created September 19, 2017 02:00
Show Gist options
  • Save n8ebel/ae8a2d48d248f58f96ee3d935328d8cf to your computer and use it in GitHub Desktop.
Save n8ebel/ae8a2d48d248f58f96ee3d935328d8cf to your computer and use it in GitHub Desktop.
Image loading binding adapter
  • we use a binding adapter for loading an image
  • this adapter also then handles setting the error state and modifying scaleTypes to fit our data
  • This has made it easy to handle image loading in a consistent fashion while allowing view models to stay independent of that control
@BindingAdapter("imageUrl")
fun setProgress(view: ImageView, url: String) {
Glide.with(view.context)
.load(url)
.error(R.drawable.ic_broken_image_black)
.listener(object : RequestListener<String, GlideDrawable> {
override fun onException(e: Exception?, model: String?, target: Target<GlideDrawable>?, isFirstResource: Boolean): Boolean {
// use this to ensure placeholder icon doesn't stretch to fill too much space
view.scaleType = ImageView.ScaleType.CENTER_INSIDE
return false
}
override fun onResourceReady(resource: GlideDrawable?, model: String?, target: Target<GlideDrawable>?, isFromMemoryCache: Boolean, isFirstResource: Boolean): Boolean {
view.scaleType = ImageView.ScaleType.FIT_CENTER
return false
}
})
.into(view)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment