Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ImaginativeShohag/8df969bdd4fe7c2b2c16e8e49ede9121 to your computer and use it in GitHub Desktop.
Save ImaginativeShohag/8df969bdd4fe7c2b2c16e8e49ede9121 to your computer and use it in GitHub Desktop.
Why Not! Image Carousel! - V2 - Custom View
carousel.carouselListener = object : CarouselListener {
override fun onCreateViewHolder(
layoutInflater: LayoutInflater,
parent: ViewGroup
): ViewBinding? {
// Here, our XML layout file name is custom_item_layout.xml. So our view binding generated class name is CustomItemLayoutBinding.
return CustomItemLayoutBinding.inflate(layoutInflater, parent, false)
}
override fun onBindViewHolder(
binding: ViewBinding,
imageScaleType: ImageView.ScaleType,
item: CarouselItem,
position: Int
) {
// Cast the binding to the returned view binding class of the onCreateViewHolder() method.
val currentBinding = binding as CustomItemLayoutBinding
// Do the bindings...
currentBinding.imageView.apply {
scaleType = imageScaleType
// setImage() is an extension function to load image to an ImageView using CarouselItem object. We need to provide current CarouselItem data and the place holder Drawable or drawable resource id to the function. placeholder parameter is optional.
setImage(item, R.drawable.ic_wb_cloudy_with_padding)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment