diff options
| author | 2013-03-21 17:08:38 -0700 | |
|---|---|---|
| committer | 2013-03-21 17:08:38 -0700 | |
| commit | 2a93911b87f7aac6d0dcceabc5983e81befeeda5 (patch) | |
| tree | ee1555bbc81d5fd7f8703eab8249589314a18a4b | |
| parent | 1ad11b9c6c7b555c27b917f26ecc08446f589284 (diff) | |
Fix a stability regression in ListView arrow scrolling
The earlier patch 3cf7b3c59 introduced a stability regression. Perform
the appropriate bounds checking alongside the fix it implements.
Bug 8264185
Change-Id: I943d6c05bacdd777f89243fdac97788b16639dc6
| -rw-r--r-- | core/java/android/widget/ListView.java | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/core/java/android/widget/ListView.java b/core/java/android/widget/ListView.java index 38550a261120..4b62c2d2450f 100644 --- a/core/java/android/widget/ListView.java +++ b/core/java/android/widget/ListView.java @@ -2417,6 +2417,34 @@ public class ListView extends AbsListView { } /** + * Used by {@link #arrowScrollImpl(int)} to help determine the next selected position + * to move to. This can return a position currently not represented by a view on screen + * but only in the direction given. + * + * @param selectedPos Current selected position to move from + * @param direction Direction to move in + * @return Desired selected position after moving in the given direction + */ + private final int nextSelectedPositionForDirection(int selectedPos, int direction) { + int nextSelected; + if (direction == View.FOCUS_DOWN) { + nextSelected = selectedPos != INVALID_POSITION && selectedPos >= mFirstPosition ? + selectedPos + 1 : + mFirstPosition; + } else { + final int lastPos = mFirstPosition + getChildCount() - 1; + nextSelected = selectedPos != INVALID_POSITION && selectedPos < lastPos? + selectedPos - 1 : + lastPos; + } + + if (nextSelected < 0 || nextSelected >= mAdapter.getCount()) { + return INVALID_POSITION; + } + return lookForSelectablePosition(nextSelected, direction == View.FOCUS_DOWN); + } + + /** * Handle an arrow scroll going up or down. Take into account whether items are selectable, * whether there are focusable items etc. * @@ -2431,9 +2459,7 @@ public class ListView extends AbsListView { View selectedView = getSelectedView(); int selectedPos = mSelectedPosition; - int nextSelectedPosition = (direction == View.FOCUS_DOWN) ? - lookForSelectablePosition(selectedPos + 1, true) : - lookForSelectablePosition(selectedPos - 1, false); + int nextSelectedPosition = nextSelectedPositionForDirection(selectedPos, direction); int amountToScroll = amountToScroll(direction, nextSelectedPosition); // if we are moving focus, we may OVERRIDE the default behavior |