ViewPager调用SetCurrentItem()方法,跨页面跳转时闪屏的问题

来源:互联网 发布:复利的威力 知乎 编辑:程序博客网 时间:2024/06/05 03:50

场景:
ViewPager左右滑动需要滑动动画,但在调用setCurrentItem()跨页面滑动时不需要显示滑动动画,要平滑静态滑动

解决:
重写ViewPager的setCurrentItem方法,当跨页面滑动时,设置duration(scroller.setmDuration(0);)

 private FixedSpeedScroller scroller; try {            Field field = ViewPager.class.getDeclaredField("mScroller");            field.setAccessible(true);            scroller = new FixedSpeedScroller(getContext(),                    new DecelerateInterpolator());            field.set(this, scroller);            scroller.setmDuration(300);        } catch (Exception e) {            e.printStackTrace();        }
    @Override    public void setCurrentItem(int item) {        if (Math.abs(getCurrentItem() - item) > 1) {            scroller.setmDuration(0);            super.setCurrentItem(item);            scroller.setmDuration(300);        } else {            scroller.setmDuration(300);            super.setCurrentItem(item);        }    }

PS:
来源:http://www.open-open.com/lib/view/open1476773958602.html
setCurrentItem(position,false),无动画,但界面显示异常,待解

0 0
原创粉丝点击