ImaginativeShohag / Why-Not-Image-Carousel

Why Not use Image Carousel if you have lots of images to show!
https://imaginativeshohag.github.io/Why-Not-Image-Carousel/
Apache License 2.0
448 stars 66 forks source link

How to prevent scrolling multiple items, and only scroll one item at a time ? #92

Open evoionosp opened 1 year ago

ImaginativeShohag commented 1 year ago

Can you give me more context? Video and/or code. You can check out the samples for the best ways to implement.

rethinavelsmitch commented 5 months ago

@ImaginativeShohag This is the video for reference. How can I scroll only one item and prevent multiple items.?

Here is my source code:

 <org.imaginativeworld.whynotimagecarousel.ImageCarousel
            android:id="@+id/carousel"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            app:carouselBackground="@android:color/transparent"
            app:carouselGravity="CENTER"
            app:carouselPaddingBottom="0dp"
            app:carouselPaddingTop="0dp"
            app:carouselType="BLOCK"
            app:infiniteCarousel="true"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/ht_pgm_overview_title"
            app:scaleOnScroll="true"
            app:scalingFactor="0.25"
            app:showBottomShadow="false"
            app:showCaption="false"
            app:autoWidthFixing="true"
            app:showIndicator="false"
            app:showNavigationButtons="false"
            app:showTopShadow="false"
            app:touchToPause="true" />

` val carousel: ImageCarousel = binding.carousel carousel.registerLifecycle(lifecycle)

    val list = mutableListOf<CarouselItem>()
    // (0..5).forEach { index ->
    (0..4).forEach { index ->

        val item = if (index < currentLevel)
            levelImageArray[index]
        else levelImageArrayDisabled[index]

        list.add(CarouselItem(item, index.toString()))
    }
    carousel.setData(list)

    carousel.onScrollListener = object : CarouselOnScrollListener {
        override fun onScrollStateChanged(
            recyclerView: RecyclerView,
            newState: Int,
            position: Int,
            carouselItem: CarouselItem?,
        ) {
            super.onScrollStateChanged(recyclerView, newState, position, carouselItem)
            binding.tasksViewPager.currentItem = position
            setRangeSliderValues(currentLevel, position)
         }
    }

    carousel.carouselListener = object : CarouselListener {
        override fun onCreateViewHolder(
            layoutInflater: LayoutInflater,
            parent: ViewGroup,
        ): ViewBinding {
            return FragmentHtLevelIndicatorBinding.inflate(layoutInflater, parent, false)
        }

        override fun onBindViewHolder(binding: ViewBinding, item: CarouselItem, position: Int) {
            val currentBinding = binding as FragmentHtLevelIndicatorBinding
            setImage(currentBinding.imgLevel, item.imageDrawable ?: R.drawable.ht_level_one)
            val level = item.caption?.toInt()?.plus(1)
            currentBinding.txtLevel.text = getString(R.string.level_with_value, level)

            if (position < currentLevel) {
                currentBinding.txtLevel.setTextColor(getColor(R.color.yellow_deep_dark))
            } else {
                currentBinding.txtLevel.setTextColor(getColor(R.color.grey_8_opacity))
            }
        }
    }`

https://github.com/ImaginativeShohag/Why-Not-Image-Carousel/assets/95007132/ab0a5433-ed71-4294-b2b9-04b3a9e7b29d