Skip to content

Commit 81e829e

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Fix bug #6213159 FocusFinder should be able to take care of Views direction"
2 parents 56485fe + 702e8f9 commit 81e829e

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

core/java/android/view/FocusFinder.java

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,45 @@ public final View findNextFocus(ViewGroup root, View focused, int direction) {
7979
switch (direction) {
8080
case View.FOCUS_RIGHT:
8181
case View.FOCUS_DOWN:
82+
setFocusBottomRight(root);
83+
break;
8284
case View.FOCUS_FORWARD:
83-
final int rootTop = root.getScrollY();
84-
final int rootLeft = root.getScrollX();
85-
mFocusedRect.set(rootLeft, rootTop, rootLeft, rootTop);
85+
if (focused != null && focused.isLayoutRtl()) {
86+
setFocusTopLeft(root);
87+
} else {
88+
setFocusBottomRight(root);
89+
}
8690
break;
8791

8892
case View.FOCUS_LEFT:
8993
case View.FOCUS_UP:
94+
setFocusTopLeft(root);
95+
break;
9096
case View.FOCUS_BACKWARD:
91-
final int rootBottom = root.getScrollY() + root.getHeight();
92-
final int rootRight = root.getScrollX() + root.getWidth();
93-
mFocusedRect.set(rootRight, rootBottom,
94-
rootRight, rootBottom);
97+
if (focused != null && focused.isLayoutRtl()) {
98+
setFocusBottomRight(root);
99+
} else {
100+
setFocusTopLeft(root);
95101
break;
102+
}
96103
}
97104
}
98105
return findNextFocus(root, focused, mFocusedRect, direction);
99106
}
100107

108+
private void setFocusTopLeft(ViewGroup root) {
109+
final int rootBottom = root.getScrollY() + root.getHeight();
110+
final int rootRight = root.getScrollX() + root.getWidth();
111+
mFocusedRect.set(rootRight, rootBottom,
112+
rootRight, rootBottom);
113+
}
114+
115+
private void setFocusBottomRight(ViewGroup root) {
116+
final int rootTop = root.getScrollY();
117+
final int rootLeft = root.getScrollX();
118+
mFocusedRect.set(rootLeft, rootTop, rootLeft, rootTop);
119+
}
120+
101121
/**
102122
* Find the next view to take focus in root's descendants, searching from
103123
* a particular rectangle in root's coordinates.
@@ -135,22 +155,10 @@ private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int d
135155
final int count = focusables.size();
136156
switch (direction) {
137157
case View.FOCUS_FORWARD:
138-
if (focused != null) {
139-
int position = focusables.lastIndexOf(focused);
140-
if (position >= 0 && position + 1 < count) {
141-
return focusables.get(position + 1);
142-
}
143-
}
144-
return focusables.get(0);
158+
return getForwardFocusable(focused, focusables, count);
145159

146160
case View.FOCUS_BACKWARD:
147-
if (focused != null) {
148-
int position = focusables.indexOf(focused);
149-
if (position > 0) {
150-
return focusables.get(position - 1);
151-
}
152-
}
153-
return focusables.get(count - 1);
161+
return getBackwardFocusable(focused, focusables, count);
154162
}
155163
return null;
156164
}
@@ -193,6 +201,38 @@ private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int d
193201
return closest;
194202
}
195203

204+
private View getForwardFocusable(View focused, ArrayList<View> focusables, int count) {
205+
return (focused != null && focused.isLayoutRtl()) ?
206+
getPreviousFocusable(focused, focusables, count) :
207+
getNextFocusable(focused, focusables, count);
208+
}
209+
210+
private View getNextFocusable(View focused, ArrayList<View> focusables, int count) {
211+
if (focused != null) {
212+
int position = focusables.lastIndexOf(focused);
213+
if (position >= 0 && position + 1 < count) {
214+
return focusables.get(position + 1);
215+
}
216+
}
217+
return focusables.get(0);
218+
}
219+
220+
private View getBackwardFocusable(View focused, ArrayList<View> focusables, int count) {
221+
return (focused != null && focused.isLayoutRtl()) ?
222+
getNextFocusable(focused, focusables, count) :
223+
getPreviousFocusable(focused, focusables, count);
224+
}
225+
226+
private View getPreviousFocusable(View focused, ArrayList<View> focusables, int count) {
227+
if (focused != null) {
228+
int position = focusables.indexOf(focused);
229+
if (position > 0) {
230+
return focusables.get(position - 1);
231+
}
232+
}
233+
return focusables.get(count - 1);
234+
}
235+
196236
/**
197237
* Is rect1 a better candidate than rect2 for a focus search in a particular
198238
* direction from a source rect? This is the core routine that determines

0 commit comments

Comments
 (0)