0

I am trying to create an image slider using ViewPager2 with left and right previews. Everything is working perfectly, but scrolling on left and right preview items is not working.

There is a similar question, but it's not working for me as well.

sample image

Above image copied from here.

Sample XML code

<androidx.viewpager2.widget.ViewPager2 
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:clipChildren="false"
    android:paddingEnd="60dp"
    android:paddingStart="60dp" />
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
  • Does this answer your question? [Viewpager2 scrolling on preview left and right is not working](https://stackoverflow.com/questions/63667356/viewpager2-scrolling-on-preview-left-and-right-is-not-working) – Pratik Fagadiya Feb 14 '22 at 07:30
  • Nope, I have mentioned that in my question. BTW, thanks for you suggestion. – Jakir Hossain Feb 14 '22 at 07:31
  • Add some left - right margin to the Views inside the viewpager2 – Sahil Feb 14 '22 at 11:53

1 Answers1

1

Single Line Implementation with Kotlin Extention for ViewPager2

  1. Single Line Implementation in your Activity or Fragment.

    binding.viewpager2.setPreviewBothSide(R.dimen._20sdp,R.dimen._35sdp)
    
  2. Add Below Code in HorizontalMarginItemDecoration.kt file in your Project

    import android.content.Context
    import android.graphics.Rect
    import android.view.View
    import androidx.annotation.DimenRes
    import androidx.recyclerview.widget.RecyclerView
    import androidx.viewpager2.widget.ViewPager2
    
    class HorizontalMarginItemDecoration(context: Context, @DimenRes horizontalMarginInDp: Int) :
        RecyclerView.ItemDecoration() {
        private val horizontalMarginInPx: Int =
            context.resources.getDimension(horizontalMarginInDp).toInt()
    
        override fun getItemOffsets(
            outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
        ) {
            outRect.right = horizontalMarginInPx
            outRect.left = horizontalMarginInPx
        }
    }
    
    fun ViewPager2.setPreviewBothSide(@DimenRes nextItemVisibleSize: Int,@DimenRes currentItemHorizontalMargin: Int) {
        this.offscreenPageLimit = 1
        val nextItemVisiblePx = resources.getDimension(nextItemVisibleSize)
        val currentItemHorizontalMarginPx = resources.getDimension(currentItemHorizontalMargin)
        val pageTranslationX = nextItemVisiblePx + currentItemHorizontalMarginPx
        val pageTransformer = ViewPager2.PageTransformer { page: View, position: Float ->
            page.translationX = -pageTranslationX * position
            page.scaleY = 1 - (0.25f * kotlin.math.abs(position))
        }
        this.setPageTransformer(pageTransformer)
    
        val itemDecoration = HorizontalMarginItemDecoration(
            context,
            currentItemHorizontalMargin
        )
        this.addItemDecoration(itemDecoration)
    }
    
  3. And Yes Here you got this Amazing Output

    enter image description here

    You Can Also Check out my Answer Here

Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65