Created
September 2, 2021 04:10
-
-
Save Tsutou/31c8637665b03778473a891bc724ba0d to your computer and use it in GitHub Desktop.
ViewPager2のアイテムをカルーセル形式で表示するためのPageTransformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ViewPager2のアイテムをカルーセル形式で表示するためのPageTransformer | |
* | |
* @param pageMargin ページ間のmargin | |
* @param itemOffset 隣のアイテムを見せる幅 | |
*/ | |
class CarouselPageTransformer( | |
@DimenRes val pageMargin: Int, | |
@DimenRes val itemOffset: Int, | |
) : ViewPager2.PageTransformer { | |
override fun transformPage(page: View, position: Float) { | |
val pageMarginPx = page.context.resources.getDimensionPixelOffset(pageMargin) | |
val offsetPx = page.context.resources.getDimensionPixelOffset(itemOffset) | |
val offset = position * (2 * offsetPx + pageMarginPx) | |
page.translationX = -offset | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment