PageTransformer 转换中的 ViewPager 边距

2023-10-04移动开发问题
2

本文介绍了PageTransformer 转换中的 ViewPager 边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 ViewPager,其中的每个视图都是卡片组上卡片的表示.每张卡片的边框上都有一个使用 ViewPager 边距的阴影:

I have a ViewPager in which each of it's views is a representation of card on a deck. Each card has a shadow on the border using the ViewPager margin:

cardsViewPager.setPageMargin(getResources().getDisplayMetrics().widthPixels / 20);
cardsViewPager.setPageMarginDrawable(R.drawable.shadow);

它按预期工作.

但是,如果我添加一个 PageTransformer 以便右侧的卡片将堆叠在左侧的卡片之上:

But, if I add a PageTransformer so that the cards on the right will stack on top of the cards on the left:

public class ScalePageTransformer implements PageTransformer {  
    private final ViewPager mViewPager;

    public ScalePageTransformer(ViewPager viewPager) {
        this.mViewPager = viewPager;
    }

    @Override
    public void transformPage(View page, float position) {
        if (position <= 0) {
            int pageWidth = mViewPager.getWidth();
            final float translateValue = position * -pageWidth;
            if (translateValue > -pageWidth) {
                page.setTranslationX(translateValue);
            } else {
                page.setTranslationX(0);
            }
        }
    }
}

我这样做是:

cardsViewPager.setPageTransformer(false, new ScalePageTransformer(cardsViewPager));

但是现在,边距没有出现.如果我在 PageTransformer 上有一个缩小效果,我可以看到当前卡片何时被缩小,可绘制的边距低于屏幕上的当前卡片.这是一张描述正在发生的事情的图片:

But now, the margin does not appear. If I had a zoom out effect on the PageTransformer, I can see when the current card is being scaled down, that the margin drawable is below the current card on the screen. Here is a pic to describe what's happening:

蓝卡在红卡之上从右向左刷.由于红牌有比例变换,我们可以看到它后面的黑色可绘制的边距.

The blue card is being swiped from the right to left on top of the red card. Since the red card has a scale transformation, we can see the margin drawable in black behind it.

有没有办法强制可抽取的边距位于红牌之上?这不应该是默认行为吗?

Is there a way to force the margin drawable to be on top of the red card? Shouldn't this be the default behavior?

推荐答案

改成

cardsViewPager.setPageTransformer(true, new ScalePageTransformer(cardsViewPager));

第一个参数是绘制顺序

这篇关于PageTransformer 转换中的 ViewPager 边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)...
2024-08-12 移动开发问题
5

在 cocos2d 中平滑拖动一个 Sprite - iPhone
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)...
2024-08-12 移动开发问题
10

Cocos2D 复杂动画
Cocos2D complex animations(Cocos2D 复杂动画)...
2024-08-12 移动开发问题
6