Android ViewPager2: Horizontal Show Partial Preview of Left and Right

August 3, 2019

Setup Horizontal ViewPage2.

viewPager.apply {
    clipToPadding = false   // allow full width shown with padding
    clipChildren = false    // allow left/right item is not clipped
    offscreenPageLimit = 2  // make sure left/right item is rendered

// increase this offset to show more of left/right
val offsetPx = 30.dpToPx(resources.displayMetrics)
viewPager.setPadding(offsetPx, 0, offsetPx, 0)

// increase this offset to increase distance between 2 items
val pageMarginPx = 10.dpToPx(resources.displayMetrics)
val marginTransformer = MarginPageTransformer(pageMarginPx)

NOTE: Convert Dp to Pixel - dpToPx

NOTE: I am using androidx.viewpager2:viewpager2:1.0.0-beta02

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.