본문 바로가기

안드로이드

[Android Studio] ViewPager2 다음 페이지 보이게 하기

728x90
val currentVisibleItemPx = dpToPxSize(40)

vpMyRoute.addItemDecoration(object : RecyclerView.ItemDecoration() {
    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        outRect.right = currentVisibleItemPx
        outRect.left = currentVisibleItemPx
    }
})

val nextVisibleItemPx = dpToPxSize(20)
val pageTranslationX = nextVisibleItemPx + currentVisibleItemPx

vpMyRoute.offscreenPageLimit = 1

vpMyRoute.setPageTransformer { page, position ->
    page.translationX = -pageTranslationX * (position)
}