Skip to content

Instantly share code, notes, and snippets.

@dadouf
Created December 11, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadouf/f2ad0d625a70dff82f8c3295008c3641 to your computer and use it in GitHub Desktop.
Save dadouf/f2ad0d625a70dff82f8c3295008c3641 to your computer and use it in GitHub Desktop.
override fun onBindViewHolder(vh: VH, position: Int) {
val image = images[position]
// Resize view to respect aspect ratio
val imageAspectRatio = image.aspectRatio
val targetImageWidth: Int =
if (imageAspectRatio < maxImageAspectRatio) {
// Tall image: height = max, width adjusts
(maxImageHeight * imageAspectRatio).roundToInt()
} else {
// Wide image: width = max
maxImageWidth
}
vh.imageView.layoutParams = RecyclerView.LayoutParams(
targetImageWidth,
RecyclerView.LayoutParams.MATCH_PARENT
)
// Load image
Glide.with(vh.imageView).load(image.url).into(vh.imageView)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment