19

我有一个ViewPager我想每 5 秒自动旋转一次,同时也允许用户滑动。但是,当我设置自动更改时,页面之间的过渡动​​画发生得非常快,我希望它慢一点。

我在这里看到了这个问题的答案:

android中Viewpager控制器的速度减慢

...但他们都使用反射。有谁知道在ViewPager不使用反射的情况下减慢自动速度的任何方法?

我想过使用PageTransformer,但不确定这是否可行,而且如果用户刷卡和自动刷卡一样,它可能会影响速度?除非我能以某种方式检测到差异,然后做一个或另一个PageTransformation

4

3 回答 3

40

使用这个自定义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;
        }
    }
}
于 2015-02-03T11:11:48.477 回答
5

您创建自定义视图寻呼机

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;
        }
    }

}

您可以根据自己设置查看寻呼机的持续时间

于 2018-09-18T04:56:26.160 回答
1

使用计划计时器选项自动旋转页面。

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();
                }

            }
        });
于 2015-01-29T13:44:14.853 回答