Skip to content

Commit 333464b

Browse files
author
Chris Banes
committed
Fix #191. Much better way of detecting when the last visible item is visible.
1 parent a78c6cd commit 333464b

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

library/src/com/handmark/pulltorefresh/library/PullToRefreshAdapterViewBase.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static FrameLayout.LayoutParams convertEmptyViewLayoutParams(ViewGroup.L
5555
return newLp;
5656
}
5757

58-
private int mSavedLastVisibleIndex = -1;
58+
private boolean mLastItemVisible;
5959
private OnScrollListener mOnScrollListener;
6060
private OnLastItemVisibleListener mOnLastItemVisibleListener;
6161
private View mEmptyView;
@@ -110,23 +110,12 @@ public final void onScroll(final AbsListView view, final int firstVisibleItem, f
110110
+ ". Total Items:" + totalItemCount);
111111
}
112112

113-
// If we have a OnItemVisibleListener, do check...
113+
/**
114+
* Set whether the Last Item is Visible. lastVisibleItemIndex is a
115+
* zero-based index, so we minus one totalItemCount to check
116+
*/
114117
if (null != mOnLastItemVisibleListener) {
115-
116-
// Detect whether the last visible item has changed
117-
final int lastVisibleItemIndex = firstVisibleItem + visibleItemCount;
118-
119-
/**
120-
* Check that the last item has changed, we have any items, and that
121-
* the last item is visible. lastVisibleItemIndex is a zero-based
122-
* index, so we add one to it to check against totalItemCount.
123-
*/
124-
if (visibleItemCount > 0 && (lastVisibleItemIndex + 1) == totalItemCount) {
125-
if (lastVisibleItemIndex != mSavedLastVisibleIndex) {
126-
mSavedLastVisibleIndex = lastVisibleItemIndex;
127-
mOnLastItemVisibleListener.onLastItemVisible();
128-
}
129-
}
118+
mLastItemVisible = (totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1);
130119
}
131120

132121
// If we're showing the indicator, check positions...
@@ -140,9 +129,17 @@ public final void onScroll(final AbsListView view, final int firstVisibleItem, f
140129
}
141130
}
142131

143-
public final void onScrollStateChanged(final AbsListView view, final int scrollState) {
132+
public final void onScrollStateChanged(final AbsListView view, final int state) {
133+
/**
134+
* Check that the scrolling has stopped, and that the last item is
135+
* visible.
136+
*/
137+
if (state == OnScrollListener.SCROLL_STATE_IDLE && null != mOnLastItemVisibleListener && mLastItemVisible) {
138+
mOnLastItemVisibleListener.onLastItemVisible();
139+
}
140+
144141
if (null != mOnScrollListener) {
145-
mOnScrollListener.onScrollStateChanged(view, scrollState);
142+
mOnScrollListener.onScrollStateChanged(view, state);
146143
}
147144
}
148145

0 commit comments

Comments
 (0)