Last active
December 27, 2015 08:39
-
-
Save ixiyang/7297398 to your computer and use it in GitHub Desktop.
PullToRefresh源码阅读摘记
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean isFirstItemVisible() { | |
final Adapter adapter = mRefreshableView.getAdapter(); | |
if (null == adapter || adapter.isEmpty()) { | |
if (DEBUG) { | |
Log.d(LOG_TAG, "isFirstItemVisible. Empty View."); | |
} | |
return true; | |
} else { | |
/** | |
* This check should really just be: | |
* mRefreshableView.getFirstVisiblePosition() == 0, but PtRListView | |
* internally use a HeaderView which messes the positions up. For | |
* now we'll just add one to account for it and rely on the inner | |
* condition which checks getTop(). | |
*/ | |
if (mRefreshableView.getFirstVisiblePosition() <= 1) { | |
final View firstVisibleChild = mRefreshableView.getChildAt(0); | |
if (firstVisibleChild != null) { | |
return firstVisibleChild.getTop() >= mRefreshableView.getTop(); | |
} | |
} | |
} | |
return false; | |
} | |
private boolean isLastItemVisible() { | |
final Adapter adapter = mRefreshableView.getAdapter(); | |
if (null == adapter || adapter.isEmpty()) { | |
if (DEBUG) { | |
Log.d(LOG_TAG, "isLastItemVisible. Empty View."); | |
} | |
return true; | |
} else { | |
final int lastItemPosition = mRefreshableView.getCount() - 1; | |
final int lastVisiblePosition = mRefreshableView.getLastVisiblePosition(); | |
if (DEBUG) { | |
Log.d(LOG_TAG, "isLastItemVisible. Last Item Position: " + lastItemPosition + " Last Visible Pos: " | |
+ lastVisiblePosition); | |
} | |
/** | |
* This check should really just be: lastVisiblePosition == | |
* lastItemPosition, but PtRListView internally uses a FooterView | |
* which messes the positions up. For me we'll just subtract one to | |
* account for it and rely on the inner condition which checks | |
* getBottom(). | |
*/ | |
if (lastVisiblePosition >= lastItemPosition - 1) { | |
final int childIndex = lastVisiblePosition - mRefreshableView.getFirstVisiblePosition(); | |
final View lastVisibleChild = mRefreshableView.getChildAt(childIndex); | |
if (lastVisibleChild != null) { | |
return lastVisibleChild.getBottom() <= mRefreshableView.getBottom(); | |
} | |
} | |
} | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public final boolean onInterceptTouchEvent(MotionEvent event) { | |
if (!isPullToRefreshEnabled()) { | |
return false; | |
} | |
final int action = event.getAction(); | |
if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { | |
mIsBeingDragged = false; | |
return false; | |
} | |
if (action != MotionEvent.ACTION_DOWN && mIsBeingDragged) { | |
return true; | |
} | |
switch (action) { | |
case MotionEvent.ACTION_MOVE: { | |
// If we're refreshing, and the flag is set. Eat all MOVE events | |
if (!mScrollingWhileRefreshingEnabled && isRefreshing()) { | |
return true; | |
} | |
if (isReadyForPull()) { | |
final float y = event.getY(), x = event.getX(); | |
final float diff, oppositeDiff, absDiff; | |
// We need to use the correct values, based on scroll | |
// direction | |
switch (getPullToRefreshScrollDirection()) { | |
case HORIZONTAL: | |
diff = x - mLastMotionX; | |
oppositeDiff = y - mLastMotionY; | |
break; | |
case VERTICAL: | |
default: | |
diff = y - mLastMotionY; | |
oppositeDiff = x - mLastMotionX; | |
break; | |
} | |
absDiff = Math.abs(diff); | |
if (absDiff > mTouchSlop && (!mFilterTouchEvents || absDiff > Math.abs(oppositeDiff))) { | |
if (mMode.showHeaderLoadingLayout() && diff >= 1f && isReadyForPullStart()) { | |
mLastMotionY = y; | |
mLastMotionX = x; | |
mIsBeingDragged = true; | |
if (mMode == Mode.BOTH) { | |
mCurrentMode = Mode.PULL_FROM_START; | |
} | |
} else if (mMode.showFooterLoadingLayout() && diff <= -1f && isReadyForPullEnd()) { | |
mLastMotionY = y; | |
mLastMotionX = x; | |
mIsBeingDragged = true; | |
if (mMode == Mode.BOTH) { | |
mCurrentMode = Mode.PULL_FROM_END; | |
} | |
} | |
} | |
} | |
break; | |
} | |
case MotionEvent.ACTION_DOWN: { | |
if (isReadyForPull()) { | |
mLastMotionY = mInitialMotionY = event.getY(); | |
mLastMotionX = mInitialMotionX = event.getX(); | |
mIsBeingDragged = false; | |
} | |
break; | |
} | |
} | |
return mIsBeingDragged; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment