Привязка карусели изображений Котлина

#kotlin #android-viewbinding

Вопрос:

Я использую этот код для карусели изображений

https://github.com/ImaginativeShohag/Why-Not-Image-Carousel

Я заставляю это работать, однако я хочу использовать пользовательский макет для своей карусели

 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)
        }
    }
}
 

Однако я не знаю, как заставить его работать, используя мой собственный пользовательский макет, а именно layout_car_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardCornerRadius="0dp">

            <ImageView
                android:id="@ id/imgFoodDetails"
                android:src="@drawable/sample_food"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"/>

            <androidx.cardview.widget.CardView
                android:layout_gravity="bottom"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                app:cardCornerRadius="5dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="10dp"
                    android:orientation="horizontal">


                    <LinearLayout
                        android:layout_weight="1.6"
                        android:orientation="vertical"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content">

                        <TextView
                            android:id="@ id/tvFoodName"
                            android:textSize="15dp"
                            android:fontFamily="@font/man_semi"
                            android:text="Crispy Chicken Skin"
                            android:singleLine="true"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content">

                        </TextView>

                        <TextView
                            android:id="@ id/tvFoodDescription"
                            android:fontFamily="@font/man_reg"
                            android:text="Crispy in the Inside Juicy on the Outside"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content">

                        </TextView>

                    </LinearLayout>

                    <TextView
                        android:id="@ id/tvFoodPrice"
                        android:textColor="#f39c12"
                        android:gravity="center"
                        android:fontFamily="@font/man_bold"
                        android:text="₱300.55"
                        android:layout_weight="0.4"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"/>

                </LinearLayout>
            </androidx.cardview.widget.CardView>

        </androidx.cardview.widget.CardView>

    </FrameLayout>
</LinearLayout>
 

Это первый раз, когда я его использую, и мне это очень тяжело