我有一个ViewPager,我想每5秒自动旋转一次,同时也允许用户滑动。但是,当我设置自动更改时,页面之间的过渡动画发生得非常快,我希望它更慢。
我在这里看到了这个问题的答案
Slowing speed of Viewpager controller in android
但他们都使用反射。有没有人知道如何在不使用反射的情况下降低ViewPager的自动速度?
我想过使用PageTransformer,但不确定这是否会起作用,如果用户滑动和自动滑动,它可能会影响速度?除非我能以某种方式检测出差异,然后再做一个或另一个PageTransformation?
答案 0 :(得分:29)
使用此自定义View
public class ViewPagerCustomDuration extends ViewPager {
private FixedSpeedScroller mScroller = null;
public ViewPagerCustomDuration(Context context) {
super(context);
init();
}
public ViewPagerCustomDuration(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
/*
* Override the Scroller instance with our own class so we can change the
* duration
*/
private void init() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
mScroller = new FixedSpeedScroller(getContext(),
new DecelerateInterpolator());
scroller.set(this, mScroller);
} catch (Exception ignored) {
}
}
/*
* Set the factor by which the duration will change
*/
public void setScrollDuration(int duration) {
mScroller.setScrollDuration(duration);
}
private class FixedSpeedScroller extends Scroller {
private int mDuration = 500;
public FixedSpeedScroller(Context context) {
super(context);
}
public FixedSpeedScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
public void setScrollDuration(int duration) {
mDuration = duration;
}
}
}
答案 1 :(得分:2)
使用计划计时器选项自动旋转页面。
int i = 0;
static Timer timer = new Timer();
//Scroller scroll = new Scroller(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_circles);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
mPager.setCurrentItem(i);
SwitchPage(3);
}
public void SwitchPage(int seconds)
{
if(timer != null)
{
timer.cancel();
}
timer = new Timer(); // At this line a new Thread will be created
timer.schedule(new SwitchPageTask(),
2000, seconds * 2000);
// delay in milliseconds
}
class SwitchPageTask extends TimerTask
{
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(i < mAdapter.getCount())
{
i++;
mPager.setCurrentItem(i, true);
}
else
{
i=0;
mPager.setCurrentItem(i, true);
}
}
});
}
}
just cancel the timer like.. timer.cancel().. or
cancel timer from another activity or fragment activity..
textView1_page3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (SampleCirclesDefault.timer != null) {
SampleCirclesDefault.timer.cancel();
Toast.makeText(getActivity(), "timer
cancled", 1).show();
}
}
});
答案 2 :(得分:1)
您创建自定义视图寻呼机
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;
import java.lang.reflect.Field;
public class CustomViewPager extends ViewPager {
private FixedSpeedScroller mScroller = null;
private boolean enabled;
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
this.enabled = true;
init();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onTouchEvent(event);
}
init();
return false;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (this.enabled) {
return super.onInterceptTouchEvent(event);
}
return false;
}
public void setPagingEnabled(boolean enabled) {
this.enabled = enabled;
}
/*
* Override the Scroller instance with our own class so we can change the
* duration
*/
private void init() {
try {
Class<?> viewpager = ViewPager.class;
Field scroller = viewpager.getDeclaredField("mScroller");
scroller.setAccessible(true);
mScroller = new FixedSpeedScroller(getContext(),
new DecelerateInterpolator());
scroller.set(this, mScroller);
} catch (Exception ignored) {
}
}
/*
* Set the factor by which the duration will change
*/
public void setScrollDuration(int duration) {
mScroller.setScrollDuration(duration);
}
private class FixedSpeedScroller extends Scroller {
private int mDuration = 500;
public FixedSpeedScroller(Context context) {
super(context);
}
public FixedSpeedScroller(Context context, Interpolator interpolator) {
super(context, interpolator);
}
public FixedSpeedScroller(Context context, Interpolator interpolator, boolean flywheel) {
super(context, interpolator, flywheel);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy, int duration) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
@Override
public void startScroll(int startX, int startY, int dx, int dy) {
// Ignore received duration, use fixed one instead
super.startScroll(startX, startY, dx, dy, mDuration);
}
public void setScrollDuration(int duration) {
mDuration = duration;
}
}
}
您可以根据自己的需要设置查看寻呼机的持续时间