ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

前端 未结 13 1150
予麋鹿
予麋鹿 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: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();
                }
            }
    

提交回复
热议问题