Alex Rivera | Logout

ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

Asked 2012-08-14T23:25:02.253
52

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?

Edit
Report

1 Answer

16

This is what i did. I overrode the package-private method smoothScrollTo in ViewPager by putting my own custom subclass in the same package. It was being passed a value of zero, which causes the snapping behavior instead of the smooth scroll.

package android.support.v4.view;

import android.content.Context;
import android.util.AttributeSet;

public class MyViewPager extends ViewPager {

    public MyViewPager(Context context) {
        super(context);
    }

    public MyViewPager(Context context, AttributeSet attr) {
        super(context, attr);
    }

    void smoothScrollTo(int x, int y, int velocity) {
        super.smoothScrollTo(x, y, 1);
    }
}

It worked great, if you want you can calculate and provide actual velocity ISO of just 1.

answered 2013-02-08T15:45:42.470

Your Answer