문제

I am using v4 ViewPager and following the below link able to set the scrolling speed and scrolling animation using interpolator.

Slowing speed of Viewpager controller in android

I had used a linear interpolator when asked in the construtor like this

  private void setScroller() {
     try {
        Field mScroller;
        mScroller = ViewPager.class.getDeclaredField("mScroller");
        mScroller.setAccessible(true); 
        FixedSpeedScroller scroller = 
           new FixedSpeedScroller(this.getContext(), new LinearInterpolator());
        mScroller.set(this, scroller);
     } catch (NoSuchFieldException e) {
     } catch (IllegalArgumentException e) {
     } catch (IllegalAccessException e) {
     }
  }

Now as i have many pages of views in my viewpages . below to it i had added pagerindicator which consists of dots.

  O O O O O O O 

Pressing a dot set the current page with smoothScrolling. Using touch events i am able to get the right page number that i need to show. after knowing this i call this method to have a smooth scrolling.

  public void snapToPage(int whichPage) {
     int numperOfSwapsRequired = whichPage - getCurrentPageIndex();
     mScrollingDuration *= numperOfSwapsRequired; 

     if(numperOfSwapsRequired == 0) return;
     if(numperOfSwapsRequired > 0) {
        for(int i = 0; i<numperOfSwapsRequired; i++){
           setCurrentItem(getCurrentItem()+1, true);
        }
     } else {
        for(int i = 0; i>numperOfSwapsRequired; i--){
           setCurrentItem(getCurrentItem()-1, true);  
        }
     }
     mScrollingDuration = 300;
  } 

Now everything works well. But only when i select a dot which is right to the selected page dot. It properly scroll with linear animation.

But when i select dot which is left to the selected page dot. It Don't scroll. It just directly jump to the page with no scrolling animation.

So i am confused how to fix this. Is this related to interpolater or scroller.

Any help would be appreciated.

Thanks

도움이 되었습니까?

해결책

ahhhhh... It always comes to be a silly but small mistake in the end. But thank fully i caught it. I had even started reading the Scroller Google Code file :)

mScrollingDuration *= numperOfSwapsRequired; 

this number is negative when we scroll right to left causing animation time to zero.

This solved it

mScrollingDuration *= Math.abs(numperOfSwapsRequired);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top