有没有办法自定义ViewPager滚动的阈值?

alm*_*awi 9 android

我无法找到一种方法来更改ViewPager中滚动页面的触摸阈值:http: //developer.android.com/reference/android/support/v4/view/ViewPager.html

查看源代码,ViewPager有一个名为determineTargetPage的方法,它检查移动触摸手势距离>阈值范围,但看起来无法修改此范围.

关于如何控制这个阈值的任何建议(如果可能的话)?

Eri*_*ric 11

在这里走出一点点; 我毫不怀疑你可能需要调整这个概念和/或代码.

我会推荐我在上面的评论中所做的事情; 扩展ViewPager和覆盖它的公共构造函数,并调用一个克隆的自定义方法initViewPager()(在您提供的源中看到).然而,正如你提到的,mFlingDistance,mMinimumVelocity,和mMaximumVelocityprivate字段,因此它们不能被一个子类进行访问.

(注意:如果您愿意,也可以调用构造函数更改这些方法.)

这里有点棘手.在Android中,我们可以使用Java Reflection API来访问这些字段.

它应该是这样的:

Class clss = getClass.getSuperClass();
Field flingField = clss.getDeclaredField("mFlingDistance"); // Of course create other variables for the other two fields
flingField.setAccessible(true);
flingField.setInt(this, <whatever int value you want>); // "this" must be the reference to the subclass object
Run Code Online (Sandbox Code Playgroud)

然后,使用您想要的任何值为其他两个变量重复此操作.您可能想要了解这些在源中的计算方式.

不幸的是,尽管我想建议使用Reflection来覆盖该private方法determineTargetPage(),但我认为不可能这样做 - 即使使用扩展的Reflection API也是如此.