@@ -1359,8 +1359,12 @@ public View focusSearch(View focused, int direction) {
13591359 case ACCESSIBILITY_FOCUS_FORWARD : {
13601360 // If we are the focused view try giving it to the first child.
13611361 if (focused == this ) {
1362- if (getChildCount () > 0 ) {
1363- return getChildAt (0 );
1362+ final int childCount = getChildCount ();
1363+ for (int i = 0 ; i < childCount ; i ++) {
1364+ View child = getChildAt (i );
1365+ if (child .getVisibility () == View .VISIBLE ) {
1366+ return child ;
1367+ }
13641368 }
13651369 return super .focusSearch (this , direction );
13661370 }
@@ -1371,19 +1375,24 @@ public View focusSearch(View focused, int direction) {
13711375 }
13721376 // Try to advance focus in the current item.
13731377 View currentItem = getChildAt (currentPosition - getFirstVisiblePosition ());
1374- if (currentItem instanceof ViewGroup ) {
1375- ViewGroup currentItemGroup = (ViewGroup ) currentItem ;
1376- View nextFocus = FocusFinder .getInstance ().findNextFocus (currentItemGroup ,
1377- focused , direction );
1378- if (nextFocus != null && nextFocus != currentItemGroup
1379- && nextFocus != focused ) {
1380- return nextFocus ;
1378+ if (currentItem .getVisibility () == View .VISIBLE ) {
1379+ if (currentItem instanceof ViewGroup ) {
1380+ ViewGroup currentItemGroup = (ViewGroup ) currentItem ;
1381+ View nextFocus = FocusFinder .getInstance ().findNextFocus (currentItemGroup ,
1382+ focused , direction );
1383+ if (nextFocus != null && nextFocus != currentItemGroup
1384+ && nextFocus != focused ) {
1385+ return nextFocus ;
1386+ }
13811387 }
13821388 }
13831389 // Try to move focus to the next item.
13841390 final int nextPosition = currentPosition - getFirstVisiblePosition () + 1 ;
1385- if (nextPosition < getChildCount ()) {
1386- return getChildAt (nextPosition );
1391+ for (int i = nextPosition ; i <= getLastVisiblePosition (); i ++) {
1392+ View child = getChildAt (i );
1393+ if (child .getVisibility () == View .VISIBLE ) {
1394+ return child ;
1395+ }
13871396 }
13881397 // No next item start searching from the list.
13891398 return super .focusSearch (this , direction );
@@ -1393,8 +1402,11 @@ public View focusSearch(View focused, int direction) {
13931402 // as closer to the bottom as possible.
13941403 if (focused == this ) {
13951404 final int childCount = getChildCount ();
1396- if (childCount > 0 ) {
1397- return super .focusSearch (getChildAt (childCount - 1 ), direction );
1405+ for (int i = childCount - 1 ; i >= 0 ; i --) {
1406+ View child = getChildAt (i );
1407+ if (child .getVisibility () == View .VISIBLE ) {
1408+ return super .focusSearch (child , direction );
1409+ }
13981410 }
13991411 return super .focusSearch (this , direction );
14001412 }
@@ -1410,28 +1422,39 @@ public View focusSearch(View focused, int direction) {
14101422 // in the previous item since in reverse the item contents
14111423 // get accessibility focus before the item itself.
14121424 if (currentItem == focused ) {
1413- // This list gets accessibility focus after the last first item.
1414- final int previoustPosition = currentPosition - getFirstVisiblePosition () - 1 ;
1415- if (previoustPosition < 0 ) {
1425+ currentItem = null ;
1426+ focused = null ;
1427+ // This list gets accessibility focus after the last item.
1428+ final int previousPosition = currentPosition - getFirstVisiblePosition () - 1 ;
1429+ for (int i = previousPosition ; i >= 0 ; i --) {
1430+ View child = getChildAt (i );
1431+ if (child .getVisibility () == View .VISIBLE ) {
1432+ currentItem = child ;
1433+ break ;
1434+ }
1435+ }
1436+ if (currentItem == null ) {
14161437 return this ;
14171438 }
1418- currentItem = getChildAt (previoustPosition );
1419- focused = null ;
14201439 }
14211440
1422- // Search for into the item.
1423- if (currentItem instanceof ViewGroup ) {
1424- ViewGroup currentItemGroup = (ViewGroup ) currentItem ;
1425- View nextFocus = FocusFinder .getInstance ().findNextFocus (currentItemGroup ,
1426- focused , direction );
1427- if (nextFocus != null && nextFocus != currentItemGroup
1428- && nextFocus != focused ) {
1429- return nextFocus ;
1441+ if (currentItem .getVisibility () == View .VISIBLE ) {
1442+ // Search into the item.
1443+ if (currentItem instanceof ViewGroup ) {
1444+ ViewGroup currentItemGroup = (ViewGroup ) currentItem ;
1445+ View nextFocus = FocusFinder .getInstance ().findNextFocus (currentItemGroup ,
1446+ focused , direction );
1447+ if (nextFocus != null && nextFocus != currentItemGroup
1448+ && nextFocus != focused ) {
1449+ return nextFocus ;
1450+ }
14301451 }
1452+
1453+ // If not item content wants focus we give it to the item.
1454+ return currentItem ;
14311455 }
14321456
1433- // If not item content wants focus we give it to the item.
1434- return currentItem ;
1457+ return super .focusSearch (this , direction );
14351458 }
14361459 }
14371460 return super .focusSearch (focused , direction );
0 commit comments