我试图取得与这个问题同样的效果。 我努力争取它, 然后我读了@wnafee 回答(我无法用它来做) 。
But then I struggle to implement what was sound pretty simple from the answer.
I had so much trouble with implementing it, that I might didn t understand the answer correctly, but there were too many issues of inaccessible APIs since I wasn t working in the same package of the Compatibility library.
在我尝试了一些方法(其中没有一个成功,而且相当复杂)之后,我走到了一个略微不同的方向,现在它像一个符咒一样运作。 我用了一些反思,对于从未使用过它的人来说,不要担心它确实是反省的基础。
我不确定这是不是最好的解决方案,但它对我有效,所以如果你愿意使用它,欢迎你。请阅读Wnafee的例子,因为它解释了我所做的一些事情。
为了完成这项任务,你应该遵循我的三部分解决方案。 (3至10分钟之间)
<强 > 第一部分: 强>
正如Wnafee所说,我刚刚通过拷贝将的源代码贴上,
(just make sure to copy the overscroll_edge and overscroll_glow
drawables in the AOSP /res/drawable directories to your own project
since they are internal to android)
我只做了两个很小的改变:
- I declare that the class extends EdgeEffectCompat (I called my class
EdgeEffectForEarlyVersions
). public class EdgeEffectForEarlyVersions extends EdgeEffectCompat
. The reason for doing this change is that the mLeftEdge
and mRightEdge
are of the type EdgeEffectCompat
.
- At the first line of the constructor of "my" new class I added a call to the parent constructor
super(context);
. Since there is no default constructor to EdgeEffectCompat
you have to Explicitly call the constructor.
<强 > 第二部分 强 >
此外,我还写了另一个函数。该函数的目的是,如果有一个早期版本(在ICS之前),我们希望使用我们刚刚复制的 < code> EdgeEffectForEarlyVersions 。为了达到这个目的,我使用了反省。
这就是这个函数:
private static void changeEdgeEffectCompactOnEarlyVersions(ViewPager viewPager, Context context)
{
/* In case that the version is earlier than 14 there is only empty implementation for the edge effect, therefore we change it.
* for more information look on the following links:
* 1. http://stackoverflow.com/questions/10773565/visual-indication-of-over-scroll-in-android
* 2. http://grepcode.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/view/ViewPager.java#ViewPager.0mLeftEdge
* 3. http://grepcode.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/widget/EdgeEffectCompat.java#EdgeEffectCompat
*/
if (Build.VERSION.SDK_INT < 14)
{
try
{
Class<ViewPager> viewPagerClass = ViewPager.class;
//Get the left edge field, since it is private we used getDeclaredField and not getDeclared
Field leftEdge = viewPagerClass.getDeclaredField("mLeftEdge");
leftEdge.setAccessible(true);
//Get the right edge field, since it is private we used getDeclaredField and not getDeclared
Field rightEdge = viewPagerClass.getDeclaredField("mRightEdge");
rightEdge.setAccessible(true);
EdgeEffectForEarlyVersions leftEdgeEffect = new EdgeEffectForEarlyVersions(context);
EdgeEffectForEarlyVersions rightEdgeEffect = new EdgeEffectForEarlyVersions(context);
//Set the mLeftEdge memeber of viewPager not to be the default one, but to be "our" edgeEffect
leftEdge.set(viewPager, leftEdgeEffect);
//Set the mRightEdge memeber of viewPager not to be the default one, but to be "our" edgeEffect
rightEdge.set(viewPager, rightEdgeEffect);
}
catch (Exception ex)
{
Log.e("refelection", ex.getMessage());
}
}
}
<强 > 第三部分 强 >
现在只有剩下的事可做, 就是在您有了 ViewPager Incent( ViewPager Incentral) 之后调用该函数, 仅此而已 。
我希望它能帮助别人