关闭

Viewpager自动轮播,可修改滑动速度

标签: viewpager自动轮播滑动速度无限轮播
1719人阅读 评论(1) 收藏 举报

Viewpager自动轮播,间隔时间5秒:

如果五秒内用户没有手动滑动viewpager,则自动跳转到下一页


/**
 * 配置viewPager
 */
private ExecutorService mExecutorService = Executors.newCachedThreadPool();//创建一个可缓存线程池
private int count = 0;//每次页面跳转时计数+1
private final ThreadLocal<Integer> selectcount = new ThreadLocal<Integer>() {
    @Override
    protected Integer initialValue() {
        return 0;
    }
};

public void setViewPager() {
    MyVPagerAdapter adapter = new MyVPagerAdapter(getActivity());

    mViewPager.setAdapter(adapter);
    //设置滑动速度
    try {
        Field mScroller = ViewPager.class.getDeclaredField("mScroller");
        mScroller.setAccessible(true);
        ChangeSpeedScroller scroller = new ChangeSpeedScroller(mViewPager.getContext(), new AccelerateInterpolator());
        scroller.setDuration(200);
        mScroller.set(mViewPager, scroller);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    //viewPager间隔5秒自动滑动
    final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //跳转到下一页
            mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
        }
    };
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(final int position, final float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
          
            count++;
            //开启新线程
            mExecutorService.execute(new Runnable() {
                @Override
                public void run() {
                    //启动线程时将当前的count赋值给selectcount
                    selectcount.set(count);
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //线程沉睡5秒之后如果selectcount==count,即count值未发生改变,即viewpager未发生跳转
                    if (selectcount.get() == count) {
                        //线程沉睡期间页面未发生跳转则发送消息,否则不发送此条消息
                        mHandler.sendEmptyMessage(0);
                    }
                }
            });
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });
//初始设置为第6000个item,使viewpager开始时就支持用户手动向前滑动,其实只要设置个不是太小的数就行,没人会无聊到一直往前翻页
    mViewPager.setCurrentItem(6000);
}

修改Viewpager滑动速度的工具类:

import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

/**
 * Created by DR66 on 2016/6/15.
 * 继承scroller类,可实现修改滑动速度
 * 外部调用setDuration()即可修改滑动速度
 */
public class ChangeSpeedScroller extends Scroller {
    private int mDuration=250;

    public void setDuration(int duration) {
        mDuration = duration;
    }

    public ChangeSpeedScroller(Context context){
        super(context);
    }
    public ChangeSpeedScroller(Context context, Interpolator interpolator){
        super(context,interpolator);
    }
    public ChangeSpeedScroller(Context context, Interpolator interpolator,boolean flywheel){
        super(context,interpolator,flywheel);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        super.startScroll(startX, startY, dx, dy, mDuration);
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy) {
        super.startScroll(startX, startY, dx, dy);
    }
}

viewpager适配器,轮播页面是6页,这里的特别之处就是设置getCount()为一个很大的数,保证在手机关机前viewpager仍然没有跳转到最后一页

/**
 * by Dovar66 on 2016/6/6.
 * My life my tude!
 */
public class MyVPagerAdapter extends PagerAdapter {
    private int[] img = {R.drawable.viewpager01, R.drawable.viewpager02, R.drawable.viewpager03, R.drawable.viewpager04, R.drawable.viewpager05, R.drawable.viewpager06};
    private Context mContext;

    public MyVPagerAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        return 100000;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        ImageView iv = new ImageView(mContext);
        iv.setImageResource(img[position%6]);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        container.addView(iv);
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }
}
我重新整理封装成了AutoViewPager,完整Demo查看请移步

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:34772次
    • 积分:641
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:9篇
    • 译文:0篇
    • 评论:45条
    最新评论