|
|
|
@ -22,6 +22,8 @@ import android.view.ViewConfiguration; |
|
|
|
|
import android.widget.ListView; |
|
|
|
|
|
|
|
|
|
public class ScrollerRunnable implements Runnable { |
|
|
|
|
private static final int SCROLL_DURATION = 300; |
|
|
|
|
|
|
|
|
|
private static final int MOVE_DOWN_POS = 1; |
|
|
|
|
private static final int MOVE_UP_POS = 2; |
|
|
|
|
|
|
|
|
@ -30,6 +32,7 @@ public class ScrollerRunnable implements Runnable { |
|
|
|
|
private int mMode; |
|
|
|
|
private int mTargetPos; |
|
|
|
|
private int mLastSeenPos; |
|
|
|
|
private int mScrollDuration; |
|
|
|
|
private final int mExtraScroll; |
|
|
|
|
|
|
|
|
|
public ScrollerRunnable(ListView listView) { |
|
|
|
@ -43,18 +46,23 @@ public class ScrollerRunnable implements Runnable { |
|
|
|
|
final int firstPos = mList.getFirstVisiblePosition(); |
|
|
|
|
final int lastPos = firstPos + mList.getChildCount() - 1; |
|
|
|
|
|
|
|
|
|
// int viewTravelCount = 0;
|
|
|
|
|
int viewTravelCount = 0; |
|
|
|
|
if (position <= firstPos) { |
|
|
|
|
// viewTravelCount = firstPos - position + 1;
|
|
|
|
|
viewTravelCount = firstPos - position + 1; |
|
|
|
|
mMode = MOVE_UP_POS; |
|
|
|
|
} else if (position >= lastPos) { |
|
|
|
|
// viewTravelCount = position - lastPos + 1;
|
|
|
|
|
viewTravelCount = position - lastPos + 1; |
|
|
|
|
mMode = MOVE_DOWN_POS; |
|
|
|
|
} else { |
|
|
|
|
// Already on screen, nothing to do
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (viewTravelCount > 0) { |
|
|
|
|
mScrollDuration = SCROLL_DURATION / viewTravelCount; |
|
|
|
|
} else { |
|
|
|
|
mScrollDuration = SCROLL_DURATION; |
|
|
|
|
} |
|
|
|
|
mTargetPos = position; |
|
|
|
|
mLastSeenPos = ListView.INVALID_POSITION; |
|
|
|
|
|
|
|
|
@ -81,8 +89,8 @@ public class ScrollerRunnable implements Runnable { |
|
|
|
|
|
|
|
|
|
if (lastPos == mLastSeenPos) { |
|
|
|
|
// No new views, let things keep going.
|
|
|
|
|
// mList.post(this);
|
|
|
|
|
// return;
|
|
|
|
|
mList.post(this); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final View lastView = mList.getChildAt(lastViewIndex); |
|
|
|
@ -91,7 +99,7 @@ public class ScrollerRunnable implements Runnable { |
|
|
|
|
final int lastViewPixelsShowing = listHeight - lastViewTop; |
|
|
|
|
final int extraScroll = lastPos < mList.getCount() - 1 ? mExtraScroll : mList.getPaddingBottom(); |
|
|
|
|
|
|
|
|
|
mList.smoothScrollBy(lastViewHeight - lastViewPixelsShowing + extraScroll, 0); |
|
|
|
|
mList.smoothScrollBy(lastViewHeight - lastViewPixelsShowing + extraScroll, mScrollDuration); |
|
|
|
|
|
|
|
|
|
mLastSeenPos = lastPos; |
|
|
|
|
if (lastPos < mTargetPos) { |
|
|
|
@ -103,8 +111,8 @@ public class ScrollerRunnable implements Runnable { |
|
|
|
|
case MOVE_UP_POS: { |
|
|
|
|
if (firstPos == mLastSeenPos) { |
|
|
|
|
// No new views, let things keep going.
|
|
|
|
|
// mList.post(this);
|
|
|
|
|
// return;
|
|
|
|
|
mList.post(this); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
final View firstView = mList.getChildAt(0); |
|
|
|
@ -114,7 +122,7 @@ public class ScrollerRunnable implements Runnable { |
|
|
|
|
final int firstViewTop = firstView.getTop(); |
|
|
|
|
final int extraScroll = firstPos > 0 ? mExtraScroll : mList.getPaddingTop(); |
|
|
|
|
|
|
|
|
|
mList.smoothScrollBy(firstViewTop - extraScroll, 0); |
|
|
|
|
mList.smoothScrollBy(firstViewTop - extraScroll, mScrollDuration); |
|
|
|
|
|
|
|
|
|
mLastSeenPos = firstPos; |
|
|
|
|
|
|
|
|
|