码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager切换滑动速度修改

时间:2015-05-19 18:46:20      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

ViewPager的setCurrentItem 滑动速度是写死地

下面的方法可以修改,在此以做记录

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class FixedSpeedScroller extends Scroller {
    private int mDuration = 1500;
 
    public FixedSpeedScroller(Context context) {
        super(context);
    }
 
    public FixedSpeedScroller(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }
 
    @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 setmDuration(int time) {
        mDuration = time;
    }
 
    public int getmDuration() {
        return mDuration;
    }
}

 

 

使用方法:

 

 

1
2
3
4
5
6
7
8
9
10
try {
                Field field = ViewPager.class.getDeclaredField("mScroller");
                field.setAccessible(true);
                FixedSpeedScroller scroller = new FixedSpeedScroller(mViewPager.getContext(),
                        new AccelerateInterpolator());
                field.set(mViewPager, scroller);
                scroller.setmDuration(2000);
            } catch (Exception e) {
                LogUtils.e(TAG, "", e);
            }

 

 

注意点: 
PagerAdapter 的 destroyItem 每次会删除上一个页面,导致,如果做自动切换页面时会看不到动画 .

 

解决办法:
destoryItem中不做删除view,instantiateItem中对view是否有parent做为判断条件,这种情况适用于,少量的固定的子View,比如一个Banner,banner中做自动循环播放

ViewPager切换滑动速度修改

标签:

原文地址:http://www.cnblogs.com/hudabing/p/4514982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!