Skip to content

Fix errors that occurred when using the version of the library androidx. #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,49 +30,71 @@
* <li>{@link #setSlideBorderMode(int)} set how to process when sliding at the last or first item</li>
* <li>{@link #setStopScrollWhenTouch(boolean)} set whether stop auto scroll when touching, default is true</li>
* </ul>
*
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -183,12 +206,11 @@ public void scrollOnce() {
* <li>if event is up, start auto scroll again.</li>
* </ul>
*/
@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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -320,7 +331,7 @@ public void setCycle(boolean isCycle) {

/**
* whether stop auto scroll when touching, default is true
*
*
* @return the stopScrollWhenTouch
*/
public boolean isStopScrollWhenTouch() {
Expand All @@ -329,7 +340,7 @@ public boolean isStopScrollWhenTouch() {

/**
* set whether stop auto scroll when touching, default is true
*
*
* @param stopScrollWhenTouch
*/
public void setStopScrollWhenTouch(boolean stopScrollWhenTouch) {
Expand All @@ -338,27 +349,27 @@ 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;
}

/**
* 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;
}

/**
* whether animating when auto scroll at the last or first item, default is true
*
*
* @return
*/
public boolean isBorderAnimation() {
Expand All @@ -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) {
Expand Down