try {
// Note: This sort is stable.
mSequentialFocusComparator.setRoot(root);
+ mSequentialFocusComparator.setIsLayoutRtl(root.isLayoutRtl());
Collections.sort(focusables, mSequentialFocusComparator);
} finally {
mSequentialFocusComparator.recycle();
final int count = focusables.size();
switch (direction) {
case View.FOCUS_FORWARD:
- return getForwardFocusable(root, focused, focusables, count);
+ return getNextFocusable(focused, focusables, count);
case View.FOCUS_BACKWARD:
- return getBackwardFocusable(root, focused, focusables, count);
+ return getPreviousFocusable(focused, focusables, count);
}
return focusables.get(count - 1);
}
return closest;
}
- private static View getForwardFocusable(ViewGroup root, View focused,
- ArrayList<View> focusables, int count) {
- return (root.isLayoutRtl()) ?
- getPreviousFocusable(focused, focusables, count) :
- getNextFocusable(focused, focusables, count);
- }
-
private static View getNextFocusable(View focused, ArrayList<View> focusables, int count) {
if (focused != null) {
int position = focusables.lastIndexOf(focused);
return null;
}
- private static View getBackwardFocusable(ViewGroup root, View focused,
- ArrayList<View> focusables, int count) {
- return (root.isLayoutRtl()) ?
- getNextFocusable(focused, focusables, count) :
- getPreviousFocusable(focused, focusables, count);
- }
-
private static View getPreviousFocusable(View focused, ArrayList<View> focusables, int count) {
if (focused != null) {
int position = focusables.indexOf(focused);
private final Rect mFirstRect = new Rect();
private final Rect mSecondRect = new Rect();
private ViewGroup mRoot;
+ private boolean mIsLayoutRtl;
public void recycle() {
mRoot = null;
mRoot = root;
}
+ public void setIsLayoutRtl(boolean b) {
+ mIsLayoutRtl = b;
+ }
+
public int compare(View first, View second) {
if (first == second) {
return 0;
} else if (mFirstRect.top > mSecondRect.top) {
return 1;
} else if (mFirstRect.left < mSecondRect.left) {
- return -1;
+ return mIsLayoutRtl ? 1 : -1;
} else if (mFirstRect.left > mSecondRect.left) {
- return 1;
+ return mIsLayoutRtl ? -1 : 1;
} else if (mFirstRect.bottom < mSecondRect.bottom) {
return -1;
} else if (mFirstRect.bottom > mSecondRect.bottom) {
return 1;
} else if (mFirstRect.right < mSecondRect.right) {
- return -1;
+ return mIsLayoutRtl ? 1 : -1;
} else if (mFirstRect.right > mSecondRect.right) {
- return 1;
+ return mIsLayoutRtl ? -1 : 1;
} else {
// The view are distinct but completely coincident so we consider
// them equal for our purposes. Since the sort is stable, this