简体   繁体   English

ViewPager2 堆栈 PageTransform

[英]ViewPager2 Stack PageTransform

I need some help with a PageTransformer for ViewPager2 .我需要一些有关 ViewPager2 的PageTransformer的帮助。

I have 3 pages in my ViewPager2 .我的ViewPager2中有 3 页。 Each page should be overlapped by the next.每一页都应与下一页重叠。 In addition, the pages should all be firmly in the middle and only be slidable to the side.此外,页面都应该牢牢地在中间,并且只能向侧面滑动。



Example:例子:
Page 1: blue color第 1 页:蓝色
Page 2: purple color第 2 页:紫色
Page 3: green color第 3 页:绿色

Page 1 is overlapped by Page 2 and page 2 is overlapped by Page 3.第 1 页与第 2 页重叠,第 2 页与第 3 页重叠。

I have set that the 3rd page can be seen first by setting the currentItem to 2我已经通过将 currentItem 设置为 2 来设置第 3 页可以首先看到

You can see the green page and if you push it to the right you should be able to see the purple page (even while pushing) If you then push the purple page to the right you should see the blue page And the pages should not move (except to the side of course)你可以看到绿色页面,如果你把它推到右边,你应该能够看到紫色页面(即使在推动时)如果你把紫色页面推到右边,你应该看到蓝色页面并且页面不应该移动(当然旁边除外)

Something similar to this but the overlap the other way around because i start at page 3 and without changing the scaling. 与此类似的东西,但反过来重叠,因为我从第 3 页开始并且没有更改缩放比例。 (The alpha change is totally okay and wanted ) 阿尔法变化是完全可以和想要的)

I hope someone can help me and thanks in advance我希望有人可以帮助我并提前致谢

After a few hours of trying, I have the desired result经过几个小时的尝试,我得到了想要的结果

class StackTransformer : ViewPager2.PageTransformer {
    override fun transformPage(view: View, position: Float) {
        view.apply {
            when {
                position < -1 -> {
                    alpha = 0f
                }
                position <= 0 -> {
                    alpha = 1 - position
                    translationX = width * -position
                    ViewCompat.setTranslationZ(this, -1f)
                }
                position <= 1 -> {
                    alpha = 1f
                    translationX = 0f
                    ViewCompat.setTranslationZ(this, 0f)
                }
                else -> {
                    alpha = 0f
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM