PageTransformer 转换中的 ViewPager 边距

ViewPager margin in PageTransformer transformations(PageTransformer 转换中的 ViewPager 边距)
本文介绍了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 边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)