diff --git a/src/cn/trinea/android/view/autoscrollviewpager/AutoScrollViewPager.java b/src/cn/trinea/android/view/autoscrollviewpager/AutoScrollViewPager.java index 1ea3e99..1e32b07 100644 --- a/src/cn/trinea/android/view/autoscrollviewpager/AutoScrollViewPager.java +++ b/src/cn/trinea/android/view/autoscrollviewpager/AutoScrollViewPager.java @@ -6,9 +6,10 @@ import android.content.Context; import android.os.Handler; import android.os.Message; -import android.support.v4.view.MotionEventCompat; -import android.support.v4.view.PagerAdapter; -import android.support.v4.view.ViewPager; + +import androidx.viewpager.widget.PagerAdapter; +import androidx.viewpager.widget.ViewPager; + import android.util.AttributeSet; import android.view.MotionEvent; import android.view.animation.Interpolator; @@ -29,49 +30,71 @@ *
  • {@link #setSlideBorderMode(int)} set how to process when sliding at the last or first item
  • *
  • {@link #setStopScrollWhenTouch(boolean)} set whether stop auto scroll when touching, default is true
  • * - * + * * @author Trinea 2013-12-30 */ public class AutoScrollViewPager extends ViewPager { - public static final int DEFAULT_INTERVAL = 1500; - - public static final int LEFT = 0; - public static final int RIGHT = 1; - - /** do nothing when sliding at the last or first item **/ - public static final int SLIDE_BORDER_MODE_NONE = 0; - /** cycle when sliding at the last or first item **/ - public static final int SLIDE_BORDER_MODE_CYCLE = 1; - /** deliver event to parent when sliding at the last or first item **/ - public static final int SLIDE_BORDER_MODE_TO_PARENT = 2; - - /** auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} **/ - private long interval = DEFAULT_INTERVAL; - /** auto scroll direction, default is {@link #RIGHT} **/ - private int direction = RIGHT; - /** whether automatic cycle when auto scroll reaching the last or first item, default is true **/ - private boolean isCycle = true; - /** whether stop auto scroll when touching, default is true **/ - private boolean stopScrollWhenTouch = true; - /** how to process when sliding at the last or first item, default is {@link #SLIDE_BORDER_MODE_NONE} **/ - private int slideBorderMode = SLIDE_BORDER_MODE_NONE; - /** whether animating when auto scroll at the last or first item **/ - private boolean isBorderAnimation = true; - /** scroll factor for auto scroll animation, default is 1.0 **/ - private double autoScrollFactor = 1.0; - /** scroll factor for swipe scroll animation, default is 1.0 **/ - private double swipeScrollFactor = 1.0; - - private Handler handler; - private boolean isAutoScroll = false; - private boolean isStopByTouch = false; - private float touchX = 0f, downX = 0f; - private float touchY = 0f; - - private CustomDurationScroller scroller = null; - - public static final int SCROLL_WHAT = 0; + public static final int DEFAULT_INTERVAL = 1500; + + public static final int LEFT = 0; + public static final int RIGHT = 1; + + /** + * do nothing when sliding at the last or first item + **/ + public static final int SLIDE_BORDER_MODE_NONE = 0; + /** + * cycle when sliding at the last or first item + **/ + public static final int SLIDE_BORDER_MODE_CYCLE = 1; + /** + * deliver event to parent when sliding at the last or first item + **/ + public static final int SLIDE_BORDER_MODE_TO_PARENT = 2; + + /** + * auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} + **/ + private long interval = DEFAULT_INTERVAL; + /** + * auto scroll direction, default is {@link #RIGHT} + **/ + private int direction = RIGHT; + /** + * whether automatic cycle when auto scroll reaching the last or first item, default is true + **/ + private boolean isCycle = true; + /** + * whether stop auto scroll when touching, default is true + **/ + private boolean stopScrollWhenTouch = true; + /** + * how to process when sliding at the last or first item, default is {@link #SLIDE_BORDER_MODE_NONE} + **/ + private int slideBorderMode = SLIDE_BORDER_MODE_NONE; + /** + * whether animating when auto scroll at the last or first item + **/ + private boolean isBorderAnimation = true; + /** + * scroll factor for auto scroll animation, default is 1.0 + **/ + private double autoScrollFactor = 1.0; + /** + * scroll factor for swipe scroll animation, default is 1.0 + **/ + private double swipeScrollFactor = 1.0; + + private Handler handler; + private boolean isAutoScroll = false; + private boolean isStopByTouch = false; + private float touchX = 0f, downX = 0f; + private float touchY = 0f; + + private CustomDurationScroller scroller = null; + + public static final int SCROLL_WHAT = 0; public AutoScrollViewPager(Context paramContext) { super(paramContext); @@ -93,12 +116,12 @@ private void init() { */ public void startAutoScroll() { isAutoScroll = true; - sendScrollMessage((long)(interval + scroller.getDuration() / autoScrollFactor * swipeScrollFactor)); + sendScrollMessage((long) (interval + scroller.getDuration() / autoScrollFactor * swipeScrollFactor)); } /** * start auto scroll - * + * * @param delayTimeInMills first scroll delay time */ public void startAutoScroll(int delayTimeInMills) { @@ -144,7 +167,7 @@ private void setViewPagerScroller() { Field interpolatorField = ViewPager.class.getDeclaredField("sInterpolator"); interpolatorField.setAccessible(true); - scroller = new CustomDurationScroller(getContext(), (Interpolator)interpolatorField.get(null)); + scroller = new CustomDurationScroller(getContext(), (Interpolator) interpolatorField.get(null)); scrollerField.set(this, scroller); } catch (Exception e) { e.printStackTrace(); @@ -183,12 +206,11 @@ public void scrollOnce() { *
  • if event is up, start auto scroll again.
  • * */ - @Override - public boolean dispatchTouchEvent(MotionEvent ev) { - int action = MotionEventCompat.getActionMasked(ev); + @Override + public boolean onTouchEvent(MotionEvent ev) { if (stopScrollWhenTouch) { - if ((action == MotionEvent.ACTION_DOWN) && isAutoScroll) { + if (ev.getAction() == MotionEvent.ACTION_DOWN && isAutoScroll) { isStopByTouch = true; stopAutoScroll(); } else if (ev.getAction() == MotionEvent.ACTION_UP && isStopByTouch) { @@ -219,22 +241,11 @@ public boolean dispatchTouchEvent(MotionEvent ev) { } getParent().requestDisallowInterceptTouchEvent(true); } - return super.dispatchTouchEvent(ev); + return super.onTouchEvent(ev); } } - /** - * based on https://github.com/youfacepalm comment to fix the issue - * "don't consume touch event when scroll up or down #29" - */ - if (consumeTouch) { - getParent().requestDisallowInterceptTouchEvent(true); - } else { - getParent().requestDisallowInterceptTouchEvent(false); - if (stopScrollWhenTouch) - startAutoScroll(); - } - - return super.dispatchTouchEvent(ev); + getParent().requestDisallowInterceptTouchEvent(true); + return super.onTouchEvent(ev); } private static class MyHandler extends Handler { @@ -266,7 +277,7 @@ public void handleMessage(Message msg) { /** * get auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} - * + * * @return the interval */ public long getInterval() { @@ -275,7 +286,7 @@ public long getInterval() { /** * set auto scroll time in milliseconds, default is {@link #DEFAULT_INTERVAL} - * + * * @param interval the interval to set */ public void setInterval(long interval) { @@ -284,7 +295,7 @@ public void setInterval(long interval) { /** * get auto scroll direction - * + * * @return {@link #LEFT} or {@link #RIGHT}, default is {@link #RIGHT} */ public int getDirection() { @@ -293,7 +304,7 @@ public int getDirection() { /** * set auto scroll direction - * + * * @param direction {@link #LEFT} or {@link #RIGHT}, default is {@link #RIGHT} */ public void setDirection(int direction) { @@ -302,7 +313,7 @@ public void setDirection(int direction) { /** * whether automatic cycle when auto scroll reaching the last or first item, default is true - * + * * @return the isCycle */ public boolean isCycle() { @@ -311,7 +322,7 @@ public boolean isCycle() { /** * set whether automatic cycle when auto scroll reaching the last or first item, default is true - * + * * @param isCycle the isCycle to set */ public void setCycle(boolean isCycle) { @@ -320,7 +331,7 @@ public void setCycle(boolean isCycle) { /** * whether stop auto scroll when touching, default is true - * + * * @return the stopScrollWhenTouch */ public boolean isStopScrollWhenTouch() { @@ -329,7 +340,7 @@ public boolean isStopScrollWhenTouch() { /** * set whether stop auto scroll when touching, default is true - * + * * @param stopScrollWhenTouch */ public void setStopScrollWhenTouch(boolean stopScrollWhenTouch) { @@ -338,9 +349,9 @@ public void setStopScrollWhenTouch(boolean stopScrollWhenTouch) { /** * get how to process when sliding at the last or first item - * + * * @return the slideBorderMode {@link #SLIDE_BORDER_MODE_NONE}, {@link #SLIDE_BORDER_MODE_TO_PARENT}, - * {@link #SLIDE_BORDER_MODE_CYCLE}, default is {@link #SLIDE_BORDER_MODE_NONE} + * {@link #SLIDE_BORDER_MODE_CYCLE}, default is {@link #SLIDE_BORDER_MODE_NONE} */ public int getSlideBorderMode() { return slideBorderMode; @@ -348,9 +359,9 @@ public int getSlideBorderMode() { /** * set how to process when sliding at the last or first item - * + * * @param slideBorderMode {@link #SLIDE_BORDER_MODE_NONE}, {@link #SLIDE_BORDER_MODE_TO_PARENT}, - * {@link #SLIDE_BORDER_MODE_CYCLE}, default is {@link #SLIDE_BORDER_MODE_NONE} + * {@link #SLIDE_BORDER_MODE_CYCLE}, default is {@link #SLIDE_BORDER_MODE_NONE} */ public void setSlideBorderMode(int slideBorderMode) { this.slideBorderMode = slideBorderMode; @@ -358,7 +369,7 @@ public void setSlideBorderMode(int slideBorderMode) { /** * whether animating when auto scroll at the last or first item, default is true - * + * * @return */ public boolean isBorderAnimation() { @@ -367,7 +378,7 @@ public boolean isBorderAnimation() { /** * set whether animating when auto scroll at the last or first item, default is true - * + * * @param isBorderAnimation */ public void setBorderAnimation(boolean isBorderAnimation) {