結論:在ViewGroup中我們可以重新實現addFocusables,已統一處理判斷View是否可以獲取到焦點.
android一般都是手機或者平板,一般都是點擊的時候獲取焦點,當我們添加遙控或手柄支持焦點移動時,這個時候焦點的查找就比較明顯了,那么Android的焦點是怎么查找的呢。
我們從handleImeFinishedEvent(ViewRootImpl.java)開始了解焦點的查找流程,handleImeFinishedEven是由dispatchImeFinishedEvent觸發,dispatchImeFinishedEvent又是由InputMethodManager觸發來的,
handleImeFinishedEvent中跟焦點相關的代碼:
- if (direction != 0) {
- View focused = mView.findFocus();//當前擁有焦點的控件
- if (focused != null) {
- View v = focused.focusSearch(direction);//根據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);
- }
- if (v.requestFocus(direction, mTempRect)) {//請求焦點網
- playSoundEffect(SoundEffectConstants
- .getContantForFocusDirection(direction));
- finishInputEvent(q, true);
- return;
- }
- }
- // Give the focused view a last chance to handle the dpad key.
- if (mView.dispatchUnhandledMove(focused, direction)) {//以前的控件 焦點改變事件
- finishInputEvent(q, true);
- return;
- }
- }
這里先獲取當前焦點控件,然后根據direction獲取下一個最佳的控件,獲取控件后調用他的requestFocus,並給前面的焦點控件一個機會處理失去焦點事件,看一下focusSearch
- public View focusSearch(int direction) {
- if (mParent != null) {//父控件不為空,調用它的focusSearch
- return mParent.focusSearch(this, direction);
- } else {
- return null;
- }
- }
一直調用parent的focusSearch,最終到
- public View focusSearch(View focused, int direction) {
- if (isRootNamespace()) {//已經是Root層 (installDecor mDecor.setIsRootNamespace(true);)
- // 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) {//繼續調用父控件的focusSearch
- return mParent.focusSearch(focused, direction);
- }
- return null;
- }
如果已經是根控件,調用FocusFinder的findNextFocus,最終調用它的findNextFocus
- private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int direction) {
- View next = null;
- if (focused != null) {
- next = findNextUserSpecifiedFocus(root, focused, direction);//是xml里通過android:nextFocusUp="..."等或者代碼特別指定的焦點順序
- }
- if (next != null) {//已經找到
- return next;
- }
- ArrayList<View> focusables = mTempList;//mTempList
- try {
- focusables.clear();
- root.addFocusables(focusables, direction);//獲取所有可以獲取焦點的控件
- if (!focusables.isEmpty()) {
- next = findNextFocus(root, focused, focusedRect, direction, focusables);//查找下一個焦點控件
- }
- } finally {
- focusables.clear();
- }
- return next;
- }
先看一下該控件是否已經設置過它的焦點移動事件,indNextUserSpecifiedFocus就是干這個事的,此方法先去判斷特定Id值是否存在,若存在則查詢出Id對應的view.其實這些Id就是xml里通過android:nextFocusUp="..."等或者代碼特別指定的焦點順序.所以在此過程先判斷,若存在,說明下個焦點已經找到,直接返回.,未找到,則調用findNextFocus繼續查找
- private View findNextFocus(ViewGroup root, View focused, Rect focusedRect,
- int direction, ArrayList<View> focusables) {
- if (focused != null) {
- if (focusedRect == null) {
- focusedRect = mFocusedRect;//焦點控件大小
- }
- // fill in interesting rect from focused
- focused.getFocusedRect(focusedRect);
- root.offsetDescendantRectToMyCoords(focused, focusedRect);
- } else {
- 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);
- }
- }
調用findNextFocusInAbsoluteDirection查找下一個焦點控件
- 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);//設置mBestCandidateRect
- switch(direction) {
- case View.FOCUS_LEFT:
- 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);
- if (isBetterCandidate(direction, focusedRect, mOtherRect, mBestCandidateRect)) {//比較和Best哪個更好
- mBestCandidateRect.set(mOtherRect);
- closest = focusable;//更合適
- }
- }
- return closest;//返回
- }
根據焦點控件的區域去查找一個合適的,具體查找,比較那個合適比較復雜,暫時還沒看懂。
關於移動的時候有時候沒有焦點,我們可以查看在findNextFocus(ViewGroup root, View focused, Rect focusedRect, int direction)這個函數中,root.addFocusables(focusables, direction);