ViewPager使用笔记

1.ViewPager.setCurrentItem(position),即使已设置动画,但是没有动画效果

原因:因为ViewPager滑动之前的时间间隔太短,可以通过反射,去修改ViewPager自动滑动时间,代码实现如下

 1 public class ViewPagerScroller extends Scroller {
 2 
 3     /**ViewPager滑动时间*/
 4     private int mScrollDuration = 0;
 5 
 6     public ViewPagerScroller(Context context, int scrollDuration){
 7         super(context);
 8         mScrollDuration = scrollDuration;
 9     }
10 
11     public static void setViewPagerScrollDuration(ViewPager viewPager, int scrollDuration){
12         try {
13             Field mScroller = ViewPager.class.getDeclaredField("mScroller");
14             mScroller.setAccessible(true);
15             ViewPagerScroller scroller = new ViewPagerScroller(viewPager.getContext( ), scrollDuration);
16             mScroller.set(viewPager, scroller);
17         }catch(Exception e){
18             e.printStackTrace();
19         }
20     }
21 
22     @Override
23     public void startScroll(int startX, int startY, int dx, int dy, int duration) {
24         super.startScroll(startX, startY, dx, dy, mScrollDuration);
25     }
26     @Override
27     public void startScroll(int startX, int startY, int dx, int dy) {
28         super.startScroll(startX, startY, dx, dy, mScrollDuration);
29     }
30 }
View Code

 调用

ViewPagerScroller.setViewPagerScrollDuration(mViewPager, VIEW_PAGER_SCROLL_DURATION);

 

posted @ 2015-07-16 10:59  BaronZ  阅读(280)  评论(0编辑  收藏  举报