我正在编写使用 ViewPager
来托管 Fragment
s 的应用程序.
I am writing application that uses ViewPager
to host Fragment
s.
当我以编程方式更改片段时,平滑滚动功能不起作用.我使用 ViewPager.setCurrentItem(int item, boolean smoothScroll)` 方法.
When I change fragment programmatically the smooth scroll function does not work. I use ViewPager.setCurrentItem(int item, boolean smoothScroll)` method.
也许有人知道解决此错误的方法吗?也许有动画?
Maybe anyone know a workaround this bug? Maybe with animations?
我正在使用支持包.问题是无论我使用 ViewPager.setCurrentItem(2, true)
还是 ViewPager.setCurrentItem(2, false)
结果都是一样的.视图切换非常快(不流畅).
I am using support package. And the issue is that whether I use ViewPager.setCurrentItem(2, true)
or ViewPager.setCurrentItem(2, false)
the result is the same. The view switches really fast (not smoothly).
我通过创建一个使用反射覆盖 ViewPager.mScroller 的 MyViewPager 解决了这个问题.
I've fixed this by creating a MyViewPager that overrides the ViewPager.mScroller using reflection.
public class MyViewPager extends ViewPager
{
public MyViewPager( Context context, AttributeSet attrs)
{
super( context, attrs );
setMyScroller();
}
private void setMyScroller()
{
try
{
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
scroller.set(this, new MyScroller(getContext()));
} catch (Exception e)
{
e.printStackTrace();
}
}
public class MyScroller extends Scroller
{
public MyScroller(Context context)
{
super(context, new DecelerateInterpolator());
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration)
{
super.startScroll(startX, startY, dx, dy, 1000 /*1 secs*/);
}
}
}
这篇关于平滑滚动在 ViewPager 中不起作用(支持库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!