Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

I am compiling on SDK 4.03, Samsung Infuse Android 2.2, Support Library for Android 4, and using ViewPager in my app, actual swipe works fine, but when I do

viewPager.setCurrentItem(id); // , or viewPager.setCurrentItem(id, true);   

It does not smooth scroll, but switches views instantly. Although the documentation clearly states that that is the purpose setting the second argument to true. Whats up with this?

like image 235
Code Droid Avatar asked Aug 14 '12 23:08

Code Droid


1 Answers

I've fixed this by creating a MyViewPager that overrides the ViewPager.mScroller using reflection.

public class MyViewPager extends ViewPager {      public MyViewPager(Context context, AttributeSet attrs) {         super(context, attrs);         setMyScroller();     }      private void setMyScroller() {         try {             Class<?> viewpager = ViewPager.class;             Field scroller = viewpager.getDeclaredField("mScroller");             scroller.setAccessible(true);             scroller.set(this, new MyScroller(getContext()));         } catch (Exception e) {             e.printStackTrace();         }     }      public class MyScroller extends Scroller {         public MyScroller(Context context) {             super(context, new DecelerateInterpolator());         }          @Override         public void startScroll(int startX, int startY, int dx, int dy, int duration) {             super.startScroll(startX, startY, dx, dy, 1000 /*1 secs*/);         }     } } 
like image 118
Marc Van Daele Avatar answered Oct 08 '22 14:10

Marc Van Daele