- 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
Created
September 19, 2017 02:00
-
-
Save n8ebel/ae8a2d48d248f58f96ee3d935328d8cf to your computer and use it in GitHub Desktop.
Image loading binding adapter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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