Skip to content

Commit 3b398e4

Browse files
committed
bug in issue smarteist#130 fixed.
1 parent b84993a commit 3b398e4

File tree

3 files changed

+128
-117
lines changed

3 files changed

+128
-117
lines changed

autoimageslider/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ext {
1515
siteUrl = 'https://github.com/smarteist'
1616
gitUrl = 'https://github.com/smarteist/android-image-slider.git'
1717

18-
libraryVersion = '1.3.6'
18+
libraryVersion = '1.3.7'
1919
organization = 'smarteistbintray' // if you push to organization's repository.
2020
developerId = 'smarteist'
2121
developerName = 'Ali Hosseini'

autoimageslider/src/main/java/com/smarteist/autoimageslider/SliderPager.java

Lines changed: 122 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@
4343
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
4444
import androidx.customview.view.AbsSavedState;
4545
import androidx.viewpager.widget.PagerAdapter;
46+
4647
import com.smarteist.autoimageslider.InfiniteAdapter.InfinitePagerAdapter;
48+
4749
import java.lang.annotation.ElementType;
4850
import java.lang.annotation.Inherited;
4951
import java.lang.annotation.Retention;
@@ -2026,138 +2028,143 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
20262028
* If we return true, onMotionEvent will be called and we do the actual
20272029
* scrolling there.
20282030
*/
2031+
try {
2032+
final int action = ev.getAction() & MotionEvent.ACTION_MASK;
20292033

2030-
final int action = ev.getAction() & MotionEvent.ACTION_MASK;
2031-
2032-
// Always take care of the touch gesture being complete.
2033-
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
2034-
// Release the drag.
2035-
if (DEBUG) Log.v(TAG, "Intercept done!");
2036-
resetTouch();
2037-
return false;
2038-
}
2039-
2040-
// Nothing more to do here if we have decided whether or not we
2041-
// are dragging.
2042-
if (action != MotionEvent.ACTION_DOWN) {
2043-
if (mIsBeingDragged) {
2044-
if (DEBUG) Log.v(TAG, "Intercept returning true!");
2045-
return true;
2046-
}
2047-
if (mIsUnableToDrag) {
2048-
if (DEBUG) Log.v(TAG, "Intercept returning false!");
2034+
// Always take care of the touch gesture being complete.
2035+
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
2036+
// Release the drag.
2037+
if (DEBUG) Log.v(TAG, "Intercept done!");
2038+
resetTouch();
20492039
return false;
20502040
}
2051-
}
20522041

2053-
switch (action) {
2054-
case MotionEvent.ACTION_MOVE: {
2055-
/*
2056-
* mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
2057-
* whether the user has moved far enough from his original down touch.
2058-
*/
2059-
2060-
/*
2061-
* Locally do absolute value. mLastMotionY is set to the y value
2062-
* of the down event.
2063-
*/
2064-
final int activePointerId = mActivePointerId;
2065-
if (activePointerId == INVALID_POINTER) {
2066-
// If we don't have a valid id, the touch down wasn't on content.
2067-
break;
2042+
// Nothing more to do here if we have decided whether or not we
2043+
// are dragging.
2044+
if (action != MotionEvent.ACTION_DOWN) {
2045+
if (mIsBeingDragged) {
2046+
if (DEBUG) Log.v(TAG, "Intercept returning true!");
2047+
return true;
20682048
}
2069-
2070-
final int pointerIndex = ev.findPointerIndex(activePointerId);
2071-
final float x = ev.getX(pointerIndex);
2072-
final float dx = x - mLastMotionX;
2073-
final float xDiff = Math.abs(dx);
2074-
final float y = ev.getY(pointerIndex);
2075-
final float yDiff = Math.abs(y - mInitialMotionY);
2076-
if (DEBUG) Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
2077-
2078-
if (dx != 0 && !isGutterDrag(mLastMotionX, dx)
2079-
&& canScroll(this, false, (int) dx, (int) x, (int) y)) {
2080-
// Nested view has scrollable area under this point. Let it be handled there.
2081-
mLastMotionX = x;
2082-
mLastMotionY = y;
2083-
mIsUnableToDrag = true;
2049+
if (mIsUnableToDrag) {
2050+
if (DEBUG) Log.v(TAG, "Intercept returning false!");
20842051
return false;
20852052
}
2086-
if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
2087-
if (DEBUG) Log.v(TAG, "Starting drag!");
2088-
mIsBeingDragged = true;
2089-
requestParentDisallowInterceptTouchEvent(true);
2090-
setScrollState(SCROLL_STATE_DRAGGING);
2091-
mLastMotionX = dx > 0
2092-
? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
2093-
mLastMotionY = y;
2094-
setScrollingCacheEnabled(true);
2095-
} else if (yDiff > mTouchSlop) {
2096-
// The finger has moved enough in the vertical
2097-
// direction to be counted as a drag... abort
2098-
// any attempt to drag horizontally, to work correctly
2099-
// with children that have scrolling containers.
2100-
if (DEBUG) Log.v(TAG, "Starting unable to drag!");
2101-
mIsUnableToDrag = true;
2102-
}
2103-
if (mIsBeingDragged) {
2104-
// Scroll to follow the motion event
2105-
if (performDrag(x)) {
2106-
ViewCompat.postInvalidateOnAnimation(this);
2107-
}
2108-
}
2109-
break;
21102053
}
21112054

2112-
case MotionEvent.ACTION_DOWN: {
2113-
/*
2114-
* Remember location of down touch.
2115-
* ACTION_DOWN always refers to pointer index 0.
2116-
*/
2117-
mLastMotionX = mInitialMotionX = ev.getX();
2118-
mLastMotionY = mInitialMotionY = ev.getY();
2119-
mActivePointerId = ev.getPointerId(0);
2120-
mIsUnableToDrag = false;
2055+
switch (action) {
2056+
case MotionEvent.ACTION_MOVE: {
2057+
/*
2058+
* mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
2059+
* whether the user has moved far enough from his original down touch.
2060+
*/
2061+
2062+
/*
2063+
* Locally do absolute value. mLastMotionY is set to the y value
2064+
* of the down event.
2065+
*/
2066+
final int activePointerId = mActivePointerId;
2067+
if (activePointerId == INVALID_POINTER) {
2068+
// If we don't have a valid id, the touch down wasn't on content.
2069+
break;
2070+
}
21212071

2122-
mIsScrollStarted = true;
2123-
mScroller.computeScrollOffset();
2124-
if (mScrollState == SCROLL_STATE_SETTLING
2125-
&& Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {
2126-
// Let the user 'catch' the pager as it animates.
2127-
mScroller.abortAnimation();
2128-
mPopulatePending = false;
2129-
populate();
2130-
mIsBeingDragged = true;
2131-
requestParentDisallowInterceptTouchEvent(true);
2132-
setScrollState(SCROLL_STATE_DRAGGING);
2133-
} else {
2134-
completeScroll(false);
2135-
mIsBeingDragged = false;
2072+
final int pointerIndex = ev.findPointerIndex(activePointerId);
2073+
final float x = ev.getX(pointerIndex);
2074+
final float dx = x - mLastMotionX;
2075+
final float xDiff = Math.abs(dx);
2076+
final float y = ev.getY(pointerIndex);
2077+
final float yDiff = Math.abs(y - mInitialMotionY);
2078+
if (DEBUG)
2079+
Log.v(TAG, "Moved x to " + x + "," + y + " diff=" + xDiff + "," + yDiff);
2080+
2081+
if (dx != 0 && !isGutterDrag(mLastMotionX, dx)
2082+
&& canScroll(this, false, (int) dx, (int) x, (int) y)) {
2083+
// Nested view has scrollable area under this point. Let it be handled there.
2084+
mLastMotionX = x;
2085+
mLastMotionY = y;
2086+
mIsUnableToDrag = true;
2087+
return false;
2088+
}
2089+
if (xDiff > mTouchSlop && xDiff * 0.5f > yDiff) {
2090+
if (DEBUG) Log.v(TAG, "Starting drag!");
2091+
mIsBeingDragged = true;
2092+
requestParentDisallowInterceptTouchEvent(true);
2093+
setScrollState(SCROLL_STATE_DRAGGING);
2094+
mLastMotionX = dx > 0
2095+
? mInitialMotionX + mTouchSlop : mInitialMotionX - mTouchSlop;
2096+
mLastMotionY = y;
2097+
setScrollingCacheEnabled(true);
2098+
} else if (yDiff > mTouchSlop) {
2099+
// The finger has moved enough in the vertical
2100+
// direction to be counted as a drag... abort
2101+
// any attempt to drag horizontally, to work correctly
2102+
// with children that have scrolling containers.
2103+
if (DEBUG) Log.v(TAG, "Starting unable to drag!");
2104+
mIsUnableToDrag = true;
2105+
}
2106+
if (mIsBeingDragged) {
2107+
// Scroll to follow the motion event
2108+
if (performDrag(x)) {
2109+
ViewCompat.postInvalidateOnAnimation(this);
2110+
}
2111+
}
2112+
break;
21362113
}
21372114

2138-
if (DEBUG) {
2139-
Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY
2140-
+ " mIsBeingDragged=" + mIsBeingDragged
2141-
+ "mIsUnableToDrag=" + mIsUnableToDrag);
2115+
case MotionEvent.ACTION_DOWN: {
2116+
/*
2117+
* Remember location of down touch.
2118+
* ACTION_DOWN always refers to pointer index 0.
2119+
*/
2120+
mLastMotionX = mInitialMotionX = ev.getX();
2121+
mLastMotionY = mInitialMotionY = ev.getY();
2122+
mActivePointerId = ev.getPointerId(0);
2123+
mIsUnableToDrag = false;
2124+
2125+
mIsScrollStarted = true;
2126+
mScroller.computeScrollOffset();
2127+
if (mScrollState == SCROLL_STATE_SETTLING
2128+
&& Math.abs(mScroller.getFinalX() - mScroller.getCurrX()) > mCloseEnough) {
2129+
// Let the user 'catch' the pager as it animates.
2130+
mScroller.abortAnimation();
2131+
mPopulatePending = false;
2132+
populate();
2133+
mIsBeingDragged = true;
2134+
requestParentDisallowInterceptTouchEvent(true);
2135+
setScrollState(SCROLL_STATE_DRAGGING);
2136+
} else {
2137+
completeScroll(false);
2138+
mIsBeingDragged = false;
2139+
}
2140+
2141+
if (DEBUG) {
2142+
Log.v(TAG, "Down at " + mLastMotionX + "," + mLastMotionY
2143+
+ " mIsBeingDragged=" + mIsBeingDragged
2144+
+ "mIsUnableToDrag=" + mIsUnableToDrag);
2145+
}
2146+
break;
21422147
}
2143-
break;
2148+
2149+
case MotionEvent.ACTION_POINTER_UP:
2150+
onSecondaryPointerUp(ev);
2151+
break;
21442152
}
21452153

2146-
case MotionEvent.ACTION_POINTER_UP:
2147-
onSecondaryPointerUp(ev);
2148-
break;
2149-
}
2154+
if (mVelocityTracker == null) {
2155+
mVelocityTracker = VelocityTracker.obtain();
2156+
}
2157+
mVelocityTracker.addMovement(ev);
21502158

2151-
if (mVelocityTracker == null) {
2152-
mVelocityTracker = VelocityTracker.obtain();
2159+
/*
2160+
* The only time we want to intercept motion events is if we are in the
2161+
* drag mode.
2162+
*/
2163+
return mIsBeingDragged;
2164+
} catch (Exception e) {
2165+
e.printStackTrace();
2166+
return false;
21532167
}
2154-
mVelocityTracker.addMovement(ev);
2155-
2156-
/*
2157-
* The only time we want to intercept motion events is if we are in the
2158-
* drag mode.
2159-
*/
2160-
return mIsBeingDragged;
21612168
}
21622169

21632170
@Override

autoimageslider/src/main/java/com/smarteist/autoimageslider/SliderView.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ public void setCurrentPagePosition(int position) {
407407
public int getCurrentPagePosition() {
408408

409409
if (getSliderAdapter() != null) {
410-
return getSliderPager().getCurrentItem() % mPagerAdapter.getCount();
410+
if (mIsInfiniteAdapter) {
411+
return getSliderPager().getCurrentItem() % mPagerAdapter.getCount();
412+
} else {
413+
return getSliderPager().getCurrentItem();
414+
}
411415
} else {
412416
throw new NullPointerException("Adapter not set");
413417
}

0 commit comments

Comments
 (0)