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
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*/);
}
}
}
Note this variable mFirstLayout
-
it will be set true while viewpager callback onAttachedToWindow
(such as on recyclerview),so will be not smoothScroll. You should override onAttachedToWindow to control the mFirstLayout
variable. Something like this:
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
try {
Field mFirstLayout = ViewPager.class.getDeclaredField("mFirstLayout");
mFirstLayout.setAccessible(true);
mFirstLayout.set(this, false);
getAdapter().notifyDataSetChanged();
setCurrentItem(getCurrentItem());
} catch (Exception e) {
e.printStackTrace();
}
}
I called setCurrentItem
function in a handler and it worked fine for me.
new Handler().post(new Runnable() {
@Override
public void run() {
myViewPager.setCurrentItem(1, true);
}
});
Hope this helps.
try with this code:
viewPager.postDelayed(new Runnable()
{
@Override
public void run()
{
viewPager.setCurrentItem(id, true);
}
}, 100);
The ViewPager was changed a lot from revision 5 to 9. Some of those changes that might be related to your problem:
Giving that your code is supposed to work just fine, my best guess is that your support library is not up-to-date. Try updating the library if it isn't.
After wasting my whole day I found a solution set offscreenPageLimit to total no. of the page.
please follow this https://stackoverflow.com/a/54800154/9097612