show a collection of items that can be scrolled on and off the screen
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.carousel.MaskableFrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/carousel_item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?attr/selectableItemBackground"
app:shapeAppearance="?attr/shapeAppearanceCornerExtraLarge">
<ImageView
android:id="@+id/carousel_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/ic_launcher" />
</com.google.android.material.carousel.MaskableFrameLayout>
val imageRes = arrayOf(
R.drawable.image_1, R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, R.drawable.image_5, R.drawable.image_6,
R.drawable.image_7, R.drawable.image_8, R.drawable.image_9,
R.drawable.image_10
)
inner class RecyclerViewAdapter : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolderClass>(){
inner class ViewHolderClass(rowBinding: RowBinding) : RecyclerView.ViewHolder(rowBinding.root){
var carouselImageView : ImageView
init {
carouselImageView = rowBinding.carouselImageView
rowBinding.root.setOnClickListener {
activityMainBinding.imageView.setImageResource(imageRes[adapterPosition])
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolderClass {
val rowBinding = RowBinding.inflate(layoutInflater)
val viewHolderClass = ViewHolderClass(rowBinding)
return viewHolderClass
}
override fun getItemCount(): Int {
return imageRes.size
}
override fun onBindViewHolder(holder: ViewHolderClass, position: Int) {
holder.carouselImageView.setImageResource(imageRes[position])
}
}
activityMainBinding.run{
recyclerView.run{
adapter = RecyclerViewAdapter()
layoutManager = CarouselLayoutManager(FullScreenCarouselStrategy())
}
}
CarouselLayoutManager(FullScreenCarouselStrategy())
CarouselLayoutManager()