ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

前端 未结 13 1111
予麋鹿
予麋鹿 2020-12-02 13:01

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



        
相关标签:
13条回答
  • 2020-12-02 13:28

    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*/);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 13:29

    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();
                }
            }
    
    0 讨论(0)
  • 2020-12-02 13:34

    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.

    0 讨论(0)
  • 2020-12-02 13:36

    try with this code:

    viewPager.postDelayed(new Runnable()
    {
        @Override
        public void run()
        {
            viewPager.setCurrentItem(id, true);
        }
    }, 100);
    
    0 讨论(0)
  • 2020-12-02 13:40

    The ViewPager was changed a lot from revision 5 to 9. Some of those changes that might be related to your problem:

    • Bug fixes for user interface behavior in ViewPager (revision 5 and 6)
    • Fixed numerous bugs for ViewPager (revision 9)

    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.

    0 讨论(0)
  • 2020-12-02 13:40

    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

    0 讨论(0)
提交回复
热议问题