以编程方式滑动时更改ViewPager2滚动速度 [英] Change ViewPager2 Scroll Speed when sliding programmatically

查看:504
本文介绍了以编程方式滑动时更改ViewPager2滚动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有办法更改 ViewPager 程序化幻灯片的动画持续时间(在此)

I know there is way to change animation duration of ViewPager programmatical slide (here).

但是在 ViewPager2

我尝试过:

try {
        final Field scrollerField = ViewPager2.class.getDeclaredField("mScroller");
        scrollerField.setAccessible(true);
        final ResizeViewPagerScroller scroller = new ResizeViewPagerScroller(getContext());
        scrollerField.set(mViewPager, scroller);
    } catch (Exception e) {
        e.printStackTrace();
    }

IDE在"mScroller"上给了我警告:

IDE gives me warning on "mScroller":

无法解析字段"mScroller"

Cannot resolve field 'mScroller'

如果我们运行此代码,那将无法正常工作,并在下面给我们提供错误:

If we Run This code thats not going to work and give us Error below:

No field mScroller in class Landroidx/viewpager2/widget/ViewPager2; (declaration of 'androidx.viewpager2.widget.ViewPager2' appears in /data/app/{packagename}-RWJhF9Gydojax8zFyFwFXg==/base.apk)

那么我们如何实现此功能?

So how we can acheive this functionality?

推荐答案

基于此问题单 Android团队不打算为ViewPager2支持这种行为,从票证中建议使用 ViewPager2.fakeDragBy().此方法的缺点是必须提供以像素为单位的页面宽度,尽管如果页面宽度与ViewPager的宽度相同,则可以改用该值.

Based on this issue ticket Android team is not planning to support such behavior for ViewPager2, advise from the ticket is to use ViewPager2.fakeDragBy(). Disadvantage of this method is that you have to supply page width in pixels, although if your page width is the same as ViewPager's width then you can use that value instead.

这里是示例实现

fun ViewPager2.setCurrentItem(
    item: Int,
    duration: Long,
    interpolator: TimeInterpolator = AccelerateDecelerateInterpolator(),
    pagePxWidth: Int = width // Default value taken from getWidth() from ViewPager2 view
) {
    val pxToDrag: Int = pagePxWidth * (item - currentItem)
    val animator = ValueAnimator.ofInt(0, pxToDrag)
    var previousValue = 0
    animator.addUpdateListener { valueAnimator ->
        val currentValue = valueAnimator.animatedValue as Int
        val currentPxToDrag = (currentValue - previousValue).toFloat()
        fakeDragBy(-currentPxToDrag)
        previousValue = currentValue
    }
    animator.addListener(object : Animator.AnimatorListener {
        override fun onAnimationStart(animation: Animator?) { beginFakeDrag() }
        override fun onAnimationEnd(animation: Animator?) { endFakeDrag() }
        override fun onAnimationCancel(animation: Animator?) { /* Ignored */ }
        override fun onAnimationRepeat(animation: Animator?) { /* Ignored */ }
    })
    animator.interpolator = interpolator
    animator.duration = duration
    animator.start()
}


要支持 RTL ,您必须翻转提供给 ViewPager2.fakeDragBy()的值,因此从上面的示例中代替了 fakeDragBy(-currentPxToDrag) RTL 时,请使用 fakeDragBy(currentPxToDrag).


To support RTL you have to flip the value supplied to ViewPager2.fakeDragBy(), so from above example instead of fakeDragBy(-currentPxToDrag) use fakeDragBy(currentPxToDrag) when using RTL.

基于官方文档:

  • 负值向前滚动,正值向后滚动(使用RTL翻转)

  • negative values scroll forward, positive backward (flipped with RTL)

在调用 fakeDragBy()之前,先使用 beginFakeDrag(),然后再完成 endFakeDrag()

before calling fakeDragBy() use beginFakeDrag() and after you're finished endFakeDrag()

这篇关于以编程方式滑动时更改ViewPager2滚动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆