package com.marshalchen.common.ui; import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.animation.Interpolator; import com.marshalchen.common.commonUtils.logUtils.Logs; import java.lang.reflect.Field; /** * Created by cym on 14-5-8. */ public class CustomViewPager extends ViewPager { private boolean isPagingEnabled; public CustomViewPager(Context context) { super(context); // postInitViewPager(); isPagingEnabled = true; } public CustomViewPager(Context context, AttributeSet attrs) { super(context, attrs); // postInitViewPager(); isPagingEnabled = true; } public void setCustomSpeed() { postInitViewPager(); } private FixSpeedScroller mScroller = null; /** * Override the Scroller instance with our own class so we can change the * duration */ private void postInitViewPager() { try { Field scroller = ViewPager.class.getDeclaredField("mScroller"); scroller.setAccessible(true); Field interpolator = ViewPager.class.getDeclaredField("sInterpolator"); interpolator.setAccessible(true); mScroller = new FixSpeedScroller(getContext(), (Interpolator) interpolator.get(null)); scroller.set(this, mScroller); } catch (Exception e) { Logs.e(e, ""); } } /** * Set the factor by which the duration will change */ public void setScrollDurationFactor(double scrollFactor) { mScroller.setScrollDurationFactor(scrollFactor); } @Override public boolean onTouchEvent(MotionEvent event) { if (this.isPagingEnabled) { return super.onTouchEvent(event); } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (this.isPagingEnabled) { return super.onInterceptTouchEvent(event); } return false; } public void setPagingEnabled(boolean b) { this.isPagingEnabled = b; } }