Android TV開發焦點移動源碼分析 一、KeyEvent分發 二、第一次獲取焦點 三、焦點搜索

點可以理解爲選中態,在Android TV上起很重要的作用。一個視圖控件只有在獲得焦點的狀態下,才能響應按鍵的Click事件。
相對於手機上用手指點擊屏幕產生的Click事件, 在TV中通過點擊遙控器的方向鍵來控制焦點的移動。當焦點移動到目標控件上之後,按下遙控器的確定鍵,纔會觸發一個Click事件,進而去做下一步的處理
在處理焦點的時候,有一些基礎的用法需要知道。
首先,一個控件isFocusable()需要爲true纔有資格可以獲取到焦點。如果想要在觸摸模式下獲取焦點,需要通過setFocusableInTouchMode(boolean)來設置。也可以直接在xml佈局文件中指定:

android:focusable="true",
 android:focusableInTouchMode="true"

一、KeyEvent分發

keyEvent 分發過程:


而當按下遙控器的按鍵時,會產生一個按鍵事件,就是KeyEvent,包含“上”,“下”,“左”,“右”,“返回”,“確定”等指令。焦點的處理就在KeyEvent的分發當中完成。
首先,KeyEvent會流轉到ViewRootImpl中開始進行處理,具體方法是內部類ViewPostImeInputStage中的processKeyEvent

private int processKeyEvent(QueuedInputEvent q) {
    // 將系統輸入轉爲keyevent事件
    final KeyEvent event = (KeyEvent)q.mEvent;

    // 1. 先由DecorView進行按鍵事件派發
    if (mView.dispatchKeyEvent(event)) {
        return FINISH_HANDLED;
    }

    ......

    // Handle automatic focus changes.
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (groupNavigationDirection != 0) {
            if (performKeyboardGroupNavigation(groupNavigationDirection)) {
                return FINISH_HANDLED;
            }
        } else {
            //2. 處理鍵盤的上下左右的焦點查找
            if (performFocusNavigation(event)) {
                return FINISH_HANDLED;
            }
        }
    }
    return FORWARD;
}
  1. mView.dispatchKeyEvent 由DecorView進行按鍵事件派發。返回 true事件消耗,不往下執行焦點搜索與請求,返回 false,繼續往下執行。
  2. 如果事件沒有被view框架消耗,之後會通過focusSearch去找下一個焦點view

接下來先看一下KeyEvent在view框架中的分發:

  1. DecorView 的 dispatchKeyEvent 函數
public boolean dispatchKeyEvent(KeyEvent event) {
    final int keyCode = event.getKeyCode();
    final int action = event.getAction();
    final boolean isDown = action == KeyEvent.ACTION_DOWN;
    
    ......

    if (!mWindow.isDestroyed()) {
        // 這裏的cb就是activity對象,Activity實現了Window.Callback接口
        final Window.Callback cb = mWindow.getCallback();
        // cb.dispatchKeyEvent 調用的是 Activity的dispatchKeyEvent
        final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event)
            : super.dispatchKeyEvent(event);
        // 是否消耗事件
        if (handled) {
            return true;
        }
    }

    return isDown ? mWindow.onKeyDown(mFeatureId, event.getKeyCode(), event)
        : mWindow.onKeyUp(mFeatureId, event.getKeyCode(), event);
}
  1. Activity 的 dispatchKeyEvent 函數
public boolean dispatchKeyEvent(KeyEvent event) {
    onUserInteraction();

    final int keyCode = event.getKeyCode();
    if (keyCode == KeyEvent.KEYCODE_MENU &&
        mActionBar != null && mActionBar.onMenuKeyEvent(event)) {
        return true;
    }

    Window win = getWindow();
    // 調用 PhoneWindow 的 superDispatchKeyEvent
    // 裏面又調用 mDecor.superDispatchKeyEvent(event)
    // mDecor爲 DecorView.
    if (win.superDispatchKeyEvent(event)) {
        return true;
    }
    View decor = mDecor;
    if (decor == null) decor = win.getDecorView();
    return event.dispatch(this, decor != null
                          ? decor.getKeyDispatcherState() : null, this);
}

這裏也是可以做焦點控制,最好是在 event.getAction() == KeyEvent.ACTION_DOWN 進行.
因爲android 的 ViewRootlmpl 的 processKeyEvent 焦點搜索與請求的地方 進行了判斷if (event.getAction() == KeyEvent.ACTION_DOWN)

  1. DecorView 的 superDispatchKeyEvent 函數
public boolean superDispatchKeyEvent(KeyEvent event) {
    // Give priority to closing action modes if applicable.
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        final int action = event.getAction();
        // Back cancels action modes first.
        if (mPrimaryActionMode != null) {
            if (action == KeyEvent.ACTION_UP) {
                mPrimaryActionMode.finish();
            }
            return true;
        }
    }
    //DecorView繼承FrameLayout 這裏調用的是 ViewGroup.dispatchKeyEvent
    if (super.dispatchKeyEvent(event)) {
        return true;
    }
    return (getViewRootImpl() != null) && getViewRootImpl().dispatchUnhandledKeyEvent(event);
}
  1. ViewGroup 的 dispatchKeyEvent 函數
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onKeyEvent(event, 1);
    }

    if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
        == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
        // 調用 view.dispatchKeyEvent
        if (super.dispatchKeyEvent(event)) {
            return true;
        }
    } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
               == PFLAG_HAS_BOUNDS) {
        // 調用 focus view 的 dispatchKeyEvent
        if (mFocused.dispatchKeyEvent(event)) {
            return true;
        }
    }

    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
    }
    return false;
}

• 首先ViewGroup會一層一層往上執行父類的dispatchKeyEvent方法,如果返回true那麼父類的dispatchKeyEvent方法就會返回true,也就代表父類消費了該焦點事件,那麼焦點事件自然就不會往下進行分發。
• 然後ViewGroup會判斷mFocused這個view是否爲空,如果爲空就會return false,焦點繼續往下傳遞;如果不爲空,那就會return mFocused的dispatchKeyEvent方法返回的結果。這個mFocused其實是ViewGroup中當前獲取焦點的子View

  1. 最後調用 View 的 dispatchKeyEvent
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onKeyEvent(event, 0);
    }

    // 調用 mOnKeyListener onKey回調,如果這裏也沒有消耗事件,繼續往下面執行
    ListenerInfo li = mListenerInfo;
    if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
        && li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
        return true;
    }

    // 主要是處理一些回調,比如 onKeyDown,onKeyLongPress,onKeyUp等等
    if (event.dispatch(this, mAttachInfo != null
                       ? mAttachInfo.mKeyDispatchState : null, this)) {
        return true;
    }

    if (mInputEventConsistencyVerifier != null) {
        mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
    }
    return false;
}

發現執行了onKeyListener中的onKey方法,如果onKey方法返回true,那麼dispatchKeyEvent方法也會返回true
如果想要修改ViewGroup焦點事件的分發
• 重寫view的dispatchKeyEvent方法
• 給某個子view設置onKeyListener監聽

二、第一次獲取焦點

下面再來看一下如果一個頁面第一次進入,系統是如何確定焦點是定位在哪個view上的


  1. ViewRootImpl中 performTraversals方法發起焦點獲取
if (mFirst) {
    if (sAlwaysAssignFocus || !isInTouchMode()) {
        // handle first focus request
        if (DEBUG_INPUT_RESIZE) {
            Log.v(mTag, "First: mView.hasFocus()=" + mView.hasFocus());
        }
        if (mView != null) {
            if (!mView.hasFocus()) {
                // 調用 View 的 restoreDefaultFocus
                mView.restoreDefaultFocus();
                if (DEBUG_INPUT_RESIZE) {
                    Log.v(mTag, "First: requested focused view=" + mView.findFocus());
                }
            } else {
                if (DEBUG_INPUT_RESIZE) {
                    Log.v(mTag, "First: existing focused view=" + mView.findFocus());
                }
            }
        }
    } else {
        // Some views (like ScrollView) won't hand focus to descendants that aren't within
        // their viewport. Before layout, there's a good change these views are size 0
        // which means no children can get focus. After layout, this view now has size, but
        // is not guaranteed to hand-off focus to a focusable child (specifically, the edge-
        // case where the child has a size prior to layout and thus won't trigger
        // focusableViewAvailable).
        View focused = mView.findFocus();
        if (focused instanceof ViewGroup
            && ((ViewGroup) focused).getDescendantFocusability()
            == ViewGroup.FOCUS_AFTER_DESCENDANTS) {
            focused.restoreDefaultFocus();
        }
    }
}
  1. View.restoreDefaultFocus
public boolean restoreDefaultFocus() {
    return requestFocus(View.FOCUS_DOWN);
}

由於DecorView繼承自FrameLayout,這裏調用的是ViewGroup的requestFocus

  1. ViewGroup.requestFocus
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
   
    int descendantFocusability = getDescendantFocusability();

    boolean result;
    switch (descendantFocusability) {
        case FOCUS_BLOCK_DESCENDANTS:
            result = super.requestFocus(direction, previouslyFocusedRect);
            break;
        case FOCUS_BEFORE_DESCENDANTS: {
            final boolean took = super.requestFocus(direction, previouslyFocusedRect);
            result = took ? took : onRequestFocusInDescendants(direction,
                                                               previouslyFocusedRect);
            break;
        }
        case FOCUS_AFTER_DESCENDANTS: {
            // 調用 onRequestFocusInDescendants 遍歷子控件進行請求
            final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
            result = took ? took : super.requestFocus(direction, previouslyFocusedRect);
            break;
        }
        default:
            throw new IllegalStateException("descendant focusability must be "
                                            + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
                                            + "but is " + descendantFocusability);
    }
    if (result && !isLayoutValid() && ((mPrivateFlags & PFLAG_WANTS_FOCUS) == 0)) {
        mPrivateFlags |= PFLAG_WANTS_FOCUS;
    }
    return result;
}

descendantFocusability:
• FOCUS_AFTER_DESCENDANTS:先分發給Child View進行處理,如果所有的Child View都沒有處理,則自己再處理
• FOCUS_BEFORE_DESCENDANTS:ViewGroup先對焦點進行處理,如果沒有處理則分發給child View進行處理
• FOCUS_BLOCK_DESCENDANTS:ViewGroup本身進行處理,不管是否處理成功,都不會分發給ChildView進行處理
因爲 PhoneWindow 給 DecoreView 初始化時設置 了 setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS),所以這裏默認是FOCUS_AFTER_DESCENDANTS

  1. ViewGroup.onRequestFocusInDescendants 遍歷子控件
protected boolean onRequestFocusInDescendants(int direction,Rect previouslyFocusedRect) {
    int index;
    int increment;
    int end;
    int count = mChildrenCount;
    if ((direction & FOCUS_FORWARD) != 0) {
        index = 0;
        increment = 1;
        end = count;
    } else {
        index = count - 1;
        increment = -1;
        end = -1;
    }
    final View[] children = mChildren;
    for (int i = index; i != end; i += increment) {
        View child = children[i];
        if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
            // 遍歷子view調用requestFocus
            if (child.requestFocus(direction, previouslyFocusedRect)) {
                return true;
            }
        }
    }
    return false;
}
  1. 子view爭取焦點
# View.java
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
    return requestFocusNoSearch(direction, previouslyFocusedRect);
}

private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
    // 如果focusable爲false直接return
    if (!canTakeFocus()) {
        return false;
    }

    if (isInTouchMode() &&
        (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
        return false;
    }

    // need to not have any parents blocking us
    if (hasAncestorThatBlocksDescendantFocus()) {
        return false;
    }

    if (!isLayoutValid()) {
        mPrivateFlags |= PFLAG_WANTS_FOCUS;
    } else {
        clearParentsWantFocus();
    }
    // 關鍵函數
    handleFocusGainInternal(direction, previouslyFocusedRect);
    return true;
}

void handleFocusGainInternal(@FocusRealDirection int direction, Rect previouslyFocusedRect) {

    if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
        mPrivateFlags |= PFLAG_FOCUSED;
        // 獲取父佈局的老焦點.
        View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null;

        if (mParent != null) {
            // 調用requestChildFocus,告訴上一層父佈局,
            mParent.requestChildFocus(this, this);
            updateFocusedInCluster(oldFocus, direction);
        }

        if (mAttachInfo != null) {
            //全局焦點監聽的回調.
            // 調用方式: View.getViewTreeObserver().addOnGlobalFocusChangeListener
            mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, this);
        }
        // 回調處理.
        onFocusChanged(true, direction, previouslyFocusedRect);
        refreshDrawableState();
    }
}
# ViewGroup.java
public void requestChildFocus(View child, View focused) {
    if (DBG) {
        System.out.println(this + " requestChildFocus()");
    }
    if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
        return;
    }

    // Unfocus us, if necessary
    super.unFocus(focused);

    // We had a previous notion of who had focus. Clear it.
    if (mFocused != child) {
        if (mFocused != null) {
            mFocused.unFocus(focused);
        }
        // 保存焦點
        mFocused = child;
    }
    if (mParent != null) {
        // 一層一層的回調父佈局
        mParent.requestChildFocus(this, focused);
    }
}

到此第一次請求焦點的過程基本告一個段落

三、焦點搜索

焦點移動的時候,默認的情況下,會按照一種算法去找在指定移動方向上最近的鄰居。在一些情況下,焦點的移動可能跟開發者的意圖不符,這時開發者可以在佈局文件中使用下面這些XML屬性來指定下一個焦點對象:

nextFocusDown
nextFocusLeft
nextFocusRight
nextFocusUp

在KeyEvent分發中已經知道如果分發過程中event沒有被消耗,就會根據方向搜索以及請求焦點View


  1. performFocusNavigation
    dispatchKeyEvent過程中沒有view消耗keyEvent,如果event.getAction() == KeyEvent.ACTION_DOWN 則調用performFocusNavigation搜索下一個焦點
private boolean performFocusNavigation(KeyEvent event) {
    int direction = 0;

    ......

    if (direction != 0) {
        // 一層一層的查找,找到真正的焦點view
        View focused = mView.findFocus();
        if (focused != null) {
            // 調用焦點view的focusSearch進行焦點搜索
            View v = focused.focusSearch(direction);
            if (v != null && v != focused) {
                // do the math the get the interesting rect
                // of previous focused into the coord system of
                // newly focused view
                focused.getFocusedRect(mTempRect);
                if (mView instanceof ViewGroup) {
                    ((ViewGroup) mView).offsetDescendantRectToMyCoords(
                        focused, mTempRect);
                    ((ViewGroup) mView).offsetRectIntoDescendantCoords(
                        v, mTempRect);
                }
                // 調用搜索到的view的requestFocus進行焦點獲取,流程同第一次焦點獲取
                if (v.requestFocus(direction, mTempRect)) {
                    playSoundEffect(SoundEffectConstants
                                    .getContantForFocusDirection(direction));
                    return true;
                }
            }

            // 進行最後的垂死掙扎,
            // 這裏可以處理一些焦點問題或者滾動翻頁問題
            if (mView.dispatchUnhandledMove(focused, direction)) {
                return true;
            }
        } else {
            if (mView.restoreDefaultFocus()) {
                return true;
            }
        }
    }
    return false;
}
  1. 調用View的focusSearch開始搜索焦點
    View並不會直接去找焦點,而是交給它的parent去找。逐漸調用VIewGroup的focusSearch方法去搜索知道最外層的佈局。最終實際上調用的是FocusFinder.getInstance().findNextFocus
# View
public View focusSearch(@FocusRealDirection int direction) {
    if (mParent != null) {
        return mParent.focusSearch(this, direction);
    } else {
        return null;
    }
}
# ViewGroup
public View focusSearch(View focused, int direction) {
    if (isRootNamespace()) {
        // root namespace means we should consider ourselves the top of the
        // tree for focus searching; otherwise we could be focus searching
        // into other tabs.  see LocalActivityManager and TabHost for more info.
        return FocusFinder.getInstance().findNextFocus(this, focused, direction);
    } else if (mParent != null) {
        return mParent.focusSearch(focused, direction);
    }
    return null;
}
  1. FocusFinder的indNextFocus方法
private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int direction) {
    View next = null;
    ViewGroup effectiveRoot = getEffectiveRoot(root, focused);
    // 流程一:
    if (focused != null) {
        // 尋找用戶指定的下一個焦點
        next = findNextUserSpecifiedFocus(effectiveRoot, focused, direction);
    }
    //如果找到,直接返回用戶指定的焦點
    if (next != null) {
        return next;
    }
    // 流程二:
    ArrayList<View> focusables = mTempList;
    try {
        focusables.clear();
        // 把當前root下的所有direction方向上可以獲得焦點的view加入列表
        effectiveRoot.addFocusables(focusables, direction);
        if (!focusables.isEmpty()) {
            // 繼續尋找當前root下的焦點
            next = findNextFocus(effectiveRoot, focused, focusedRect, direction, focusables);
        }
    } finally {
        focusables.clear();
    }
    return next;
}

流程一:查找用戶指定的下一個焦點

1. FocusFinder findUserSetNextFocus()找到用戶指定的下一個焦點
private View findNextUserSpecifiedFocus(ViewGroup root, View focused, int direction) {
    // check for user specified next focus
    View userSetNextFocus = focused.findUserSetNextFocus(root, direction);
    View cycleCheck = userSetNextFocus;
    boolean cycleStep = true; // we want the first toggle to yield false
    while (userSetNextFocus != null) {
        // 判斷是否可以獲得焦點
        if (userSetNextFocus.isFocusable()
            && userSetNextFocus.getVisibility() == View.VISIBLE
            && (!userSetNextFocus.isInTouchMode()
                || userSetNextFocus.isFocusableInTouchMode())) {
            return userSetNextFocus;
        }
        userSetNextFocus = userSetNextFocus.findUserSetNextFocus(root, direction);
        if (cycleStep = !cycleStep) {
            cycleCheck = cycleCheck.findUserSetNextFocus(root, direction);
            if (cycleCheck == userSetNextFocus) {
                // found a cycle, user-specified focus forms a loop and none of the views
                // are currently focusable.
                break;
            }
        }
    }
    return null;
}

2. View.findUserSetNextFocus
View findUserSetNextFocus(View root, @FocusDirection int direction) {
    switch (direction) {
        case FOCUS_LEFT:
            // 如果mNextFocusLeftId == View.NO_ID 即用戶沒有指定下一個焦點直接返回null
            if (mNextFocusLeftId == View.NO_ID) return null;
            return findViewInsideOutShouldExist(root, mNextFocusLeftId);
        case FOCUS_RIGHT:
            ......
        }
    }
    return null;
}
3. View.findViewInsideOutShouldExist
private View findViewInsideOutShouldExist(View root, int id) {
    if (mMatchIdPredicate == null) {
        mMatchIdPredicate = new MatchIdPredicate();
    }
    // 要尋找的下一個焦點的view的id
    mMatchIdPredicate.mId = id;
    View result = root.findViewByPredicateInsideOut(this, mMatchIdPredicate);
    if (result == null) {
        Log.w(VIEW_LOG_TAG, "couldn't find view with id " + id);
    }
    return result;
}

4. View.findViewByPredicateInsideOut
public final <T extends View> T findViewByPredicateInsideOut(
        View start, Predicate<View> predicate) {
    View childToSkip = null;
    for (;;) {
        // 判斷一下start跟id指定的view是否是同一個,同一個直接返回
        T view = start.findViewByPredicateTraversal(predicate, childToSkip);
        if (view != null || start == this) {
            return view;
        }
        // 如果沒有找到,則一層層找到start的父view繼續比較
        ViewParent parent = start.getParent();
        if (parent == null || !(parent instanceof View)) {
            return null;
        }

        childToSkip = start;
        start = (View) parent;
    }
}

5. View.findViewByPredicateTraversal 查找子view中是否有對應id的view
protected <T extends View> T findViewByPredicateTraversal(Predicate<View> predicate,View childToSkip) {
    if (predicate.test(this)) {
        return (T) this;
    }

    final View[] where = mChildren;
    final int len = mChildrenCount;

    for (int i = 0; i < len; i++) {
        View v = where[i];

        if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) {
            v = v.findViewByPredicate(predicate);

            if (v != null) {
                return (T) v;
            }
        }
    }

    return null;
}

流程二:獲取搜索方向上所有可以獲取焦點的view,使用算法查找下一個view
addFocusables() 獲取搜索方向上可獲得焦點的view

public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
    final int focusableCount = views.size();

    final int descendantFocusability = getDescendantFocusability();
    final boolean blockFocusForTouchscreen = shouldBlockFocusForTouchscreen();
    final boolean focusSelf = (isFocusableInTouchMode() || !blockFocusForTouchscreen);

    // 覆蓋子view,自己獲取焦點
    if (descendantFocusability == FOCUS_BLOCK_DESCENDANTS) {
        if (focusSelf) {
            super.addFocusables(views, direction, focusableMode);
        }
        return;
    }

    if (blockFocusForTouchscreen) {
        focusableMode |= FOCUSABLES_TOUCH_MODE;
    }
    // 自己優先獲取焦點
    if ((descendantFocusability == FOCUS_BEFORE_DESCENDANTS) && focusSelf) {
        super.addFocusables(views, direction, focusableMode);
    }

    int count = 0;
    final View[] children = new View[mChildrenCount];
    for (int i = 0; i < mChildrenCount; ++i) {
        View child = mChildren[i];
        // 判斷view是否可見
        if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) {
            children[count++] = child;
        }
    }
    // 根據位置對children進行排序
    FocusFinder.sort(children, 0, count, this, isLayoutRtl());
    for (int i = 0; i < count; ++i) {
        children[i].addFocusables(views, direction, focusableMode);
    }

    // When set to FOCUS_AFTER_DESCENDANTS, we only add ourselves if
    // there aren't any focusable descendants.  this is
    // to avoid the focus search finding layouts when a more precise search
    // among the focusable children would be more interesting.
    if ((descendantFocusability == FOCUS_AFTER_DESCENDANTS) && focusSelf
        && focusableCount == views.size()) {
        super.addFocusables(views, direction, focusableMode);
    }
}

descendantFocusability屬性決定了ViewGroup和其子view的聚焦優先級
• FOCUS_BLOCK_DESCENDANTS:viewgroup會覆蓋子類控件而直接獲得焦點
• FOCUS_BEFORE_DESCENDANTS:viewgroup會覆蓋子類控件而直接獲得焦點
• FOCUS_AFTER_DESCENDANTS:viewgroup只有當其子類控件不需要獲取焦點時才獲取焦點
addFocusables的第一個參數views是由root決定的。在ViewGroup的focusSearch方法中傳進來的root是DecorView,也可以主動調用FocusFinder的findNextFocus方法,在指定的ViewGroup中查找焦點。
FocusFinder.findNextFocus 查找焦點

private View findNextFocus(ViewGroup root, View focused, Rect focusedRect,
                           int direction, ArrayList<View> focusables) {
    // 1. 焦點不爲空的情況
    if (focused != null) {
        if (focusedRect == null) {
            focusedRect = mFocusedRect;
        }
        // fill in interesting rect from focused
        focused.getFocusedRect(focusedRect);
        root.offsetDescendantRectToMyCoords(focused, focusedRect);
    } else {
        // 2. 焦點爲空的情況
        if (focusedRect == null) {
            focusedRect = mFocusedRect;
            // make up a rect at top left or bottom right of root
            switch (direction) {
                case View.FOCUS_RIGHT:
                case View.FOCUS_DOWN:
                    setFocusTopLeft(root, focusedRect);
                    break;
                case View.FOCUS_FORWARD:
                    if (root.isLayoutRtl()) {
                        setFocusBottomRight(root, focusedRect);
                    } else {
                        setFocusTopLeft(root, focusedRect);
                    }
                    break;

                case View.FOCUS_LEFT:
                case View.FOCUS_UP:
                    setFocusBottomRight(root, focusedRect);
                    break;
                case View.FOCUS_BACKWARD:
                    if (root.isLayoutRtl()) {
                        setFocusTopLeft(root, focusedRect);
                    } else {
                        setFocusBottomRight(root, focusedRect);
                        break;
                    }
            }
        }
    }

    switch (direction) {
        case View.FOCUS_FORWARD:
        case View.FOCUS_BACKWARD:
            return findNextFocusInRelativeDirection(focusables, root, focused, focusedRect,
                                                    direction);
        case View.FOCUS_UP:
        case View.FOCUS_DOWN:
        case View.FOCUS_LEFT:
        case View.FOCUS_RIGHT:
            return findNextFocusInAbsoluteDirection(focusables, root, focused,
                                                    focusedRect, direction);
        default:
            throw new IllegalArgumentException("Unknown direction: " + direction);
    }
}

View findNextFocusInAbsoluteDirection(ArrayList<View> focusables, ViewGroup root, View focused,
                                      Rect focusedRect, int direction) {
    // initialize the best candidate to something impossible
    // (so the first plausible view will become the best choice)
    mBestCandidateRect.set(focusedRect);
    switch(direction) {
        case View.FOCUS_LEFT:
            // mBestCandidateRect在focusedReact的右邊,並且距離focusedReact的右邊一個像素
            mBestCandidateRect.offset(focusedRect.width() + 1, 0);
            break;
        case View.FOCUS_RIGHT:
            mBestCandidateRect.offset(-(focusedRect.width() + 1), 0);
            break;
        case View.FOCUS_UP:
            mBestCandidateRect.offset(0, focusedRect.height() + 1);
            break;
        case View.FOCUS_DOWN:
            mBestCandidateRect.offset(0, -(focusedRect.height() + 1));
    }

    View closest = null;

     //遍歷可獲得焦點的列表
    int numFocusables = focusables.size();
    for (int i = 0; i < numFocusables; i++) {
        View focusable = focusables.get(i);

        // only interested in other non-root views
        if (focusable == focused || focusable == root) continue;

        // get focus bounds of other view in same coordinate system
        focusable.getFocusedRect(mOtherRect);
        root.offsetDescendantRectToMyCoords(focusable, mOtherRect);

        //找到最佳的候選的view,則返回
        if (isBetterCandidate(direction, focusedRect, mOtherRect, mBestCandidateRect)) {
            mBestCandidateRect.set(mOtherRect);
            closest = focusable;
        }
    }
    return closest;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章