@Test public void getFocusables() { Context context = InstrumentationRegistry.getTargetContext(); final LinearLayout rootView = new LinearLayout(context); final ControlBar bar = new ControlBar(context, null); rootView.addView(bar, 800, 100); final Button barSibling = new Button(context); rootView.addView(barSibling, 100, 100); final TextView v1 = new Button(context); bar.addView(v1, 100, 100); final TextView v2 = new Button(context); bar.addView(v2, 100, 100); final TextView v3 = new Button(context); bar.addView(v3, 100, 100); ArrayList<View> focusables = new ArrayList(); bar.addFocusables(focusables, View.FOCUS_DOWN); assertEquals(1, focusables.size()); assertSame(focusables.get(0), v2); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_UP); assertEquals(1, focusables.size()); assertSame(focusables.get(0), v2); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_LEFT); assertEquals(3, focusables.size()); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_RIGHT); assertEquals(3, focusables.size()); InstrumentationRegistry.getInstrumentation().runOnMainSync( new Runnable() { @Override public void run() { v3.requestFocus(View.FOCUS_DOWN); } } ); InstrumentationRegistry.getInstrumentation().runOnMainSync( new Runnable() { @Override public void run() { barSibling.requestFocus(); } } ); assertFalse(bar.hasFocus()); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_DOWN); assertEquals(1, focusables.size()); assertSame(focusables.get(0), v3); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_UP); assertEquals(1, focusables.size()); assertSame(focusables.get(0), v3); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_LEFT); assertEquals(3, focusables.size()); focusables.clear(); bar.addFocusables(focusables, View.FOCUS_RIGHT); assertEquals(3, focusables.size()); }
@Override public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { if ((direction == ViewGroup.FOCUS_UP || direction == ViewGroup.FOCUS_DOWN)) { if (mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount()) { views.add(getChildAt(mLastFocusIndex)); } else if (getChildCount() > 0) { views.add(getChildAt(getDefaultFocusIndex())); } } else { super.addFocusables(views, direction, focusableMode); } }
ControlBar extends LinearLayout { @Override public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { if ((direction == ViewGroup.FOCUS_UP || direction == ViewGroup.FOCUS_DOWN)) { if (mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount()) { views.add(getChildAt(mLastFocusIndex)); } else if (getChildCount() > 0) { views.add(getChildAt(getDefaultFocusIndex())); } } else { super.addFocusables(views, direction, focusableMode); } } }
ControlBar extends LinearLayout { @Override public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { if ((direction == ViewGroup.FOCUS_UP || direction == ViewGroup.FOCUS_DOWN)) { if (mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount()) { views.add(getChildAt(mLastFocusIndex)); } else if (getChildCount() > 0) { views.add(getChildAt(getDefaultFocusIndex())); } } else { super.addFocusables(views, direction, focusableMode); } } ControlBar(Context context, AttributeSet attrs);  ControlBar(Context context, AttributeSet attrs, int defStyle); }
ControlBar extends LinearLayout { @Override public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { if ((direction == ViewGroup.FOCUS_UP || direction == ViewGroup.FOCUS_DOWN)) { if (mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount()) { views.add(getChildAt(mLastFocusIndex)); } else if (getChildCount() > 0) { views.add(getChildAt(getDefaultFocusIndex())); } } else { super.addFocusables(views, direction, focusableMode); } } ControlBar(Context context, AttributeSet attrs);  ControlBar(Context context, AttributeSet attrs, int defStyle); @Override void addFocusables(ArrayList<View> views, int direction, int focusableMode); void setOnChildFocusedListener(OnChildFocusedListener listener); void setChildMarginFromCenter(int marginFromCenter); @Override void requestChildFocus(View child, View focused); }
ControlBar extends LinearLayout { @Override public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { if ((direction == ViewGroup.FOCUS_UP || direction == ViewGroup.FOCUS_DOWN)) { if (mLastFocusIndex >= 0 && mLastFocusIndex < getChildCount()) { views.add(getChildAt(mLastFocusIndex)); } else if (getChildCount() > 0) { views.add(getChildAt(getDefaultFocusIndex())); } } else { super.addFocusables(views, direction, focusableMode); } } ControlBar(Context context, AttributeSet attrs);  ControlBar(Context context, AttributeSet attrs, int defStyle); @Override void addFocusables(ArrayList<View> views, int direction, int focusableMode); void setOnChildFocusedListener(OnChildFocusedListener listener); void setChildMarginFromCenter(int marginFromCenter); @Override void requestChildFocus(View child, View focused);  }