Change ViewPager animation duration when sliding programmatically

前端 未结 7 1123
清酒与你
清酒与你 2020-11-28 20:45

I\'m changing slide with the following code:

viewPager.setCurrentItem(index++, true);

But it changes too fast. Is there a way to set manual

7条回答
  •  日久生厌
    2020-11-28 20:51

    Better solution is to simply access the private fields by creating the class in the support package. EDIT This is bound to the MAX_SETTLE_DURATION of 600ms, set by the ViewPagerclass.

    package android.support.v4.view;
    
    import android.content.Context;
    import android.util.AttributeSet;
    
    public class SlowViewPager extends ViewPager {
    
        // The speed of the scroll used by setCurrentItem()
        private static final int VELOCITY = 200;
    
        public SlowViewPager(Context context) {
            super(context);
        }
    
        public SlowViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        void setCurrentItemInternal(int item, boolean smoothScroll, boolean always) {
            setCurrentItemInternal(item, smoothScroll, always, VELOCITY);
        }
    
    }
    

    You can, of course, then add a custom attribute so this can be set via XML.

提交回复
热议问题