android 焦點獲取流程(轉)

結論:在ViewGroup中咱們能夠從新實現addFocusables,已統一處理判斷View是否能夠獲取到焦點.java

android通常都是手機或者平板,通常都是點擊的時候獲取焦點,當咱們添加遙控或手柄支持焦點移動時,這個時候焦點的查找就比較明顯了,那麼Android的焦點是怎麼查找的呢。android

咱們從handleImeFinishedEvent(ViewRootImpl.java)開始瞭解焦點的查找流程,handleImeFinishedEven是由dispatchImeFinishedEvent觸發,dispatchImeFinishedEvent又是由InputMethodManager觸發來的,ide

handleImeFinishedEvent中跟焦點相關的代碼:函數

 

[java]  view plain  copy
 
  1. if (direction != 0) {  
  2.                 View focused = mView.findFocus();//當前擁有焦點的控件  
  3.                 if (focused != null) {  
  4.                     View v = focused.focusSearch(direction);//根據direction查找下一個應該獲取焦點的控件  
  5.                     if (v != null && v != focused) {  
  6.                         // do the math the get the interesting rect  
  7.                         // of previous focused into the coord system of  
  8.                         // newly focused view  
  9.                         focused.getFocusedRect(mTempRect);  
  10.                         if (mView instanceof ViewGroup) {  
  11.                             ((ViewGroup) mView).offsetDescendantRectToMyCoords(  
  12.                                     focused, mTempRect);  
  13.                             ((ViewGroup) mView).offsetRectIntoDescendantCoords(  
  14.                                     v, mTempRect);  
  15.                         }  
  16.                         if (v.requestFocus(direction, mTempRect)) {//請求焦點網  
  17.                             playSoundEffect(SoundEffectConstants  
  18.                                     .getContantForFocusDirection(direction));  
  19.                             finishInputEvent(q, true);  
  20.                             return;  
  21.                         }  
  22.                     }  
  23.   
  24.                     // Give the focused view a last chance to handle the dpad key.  
  25.                     if (mView.dispatchUnhandledMove(focused, direction)) {//之前的控件 焦點改變事件  
  26.                         finishInputEvent(q, true);  
  27.                         return;  
  28.                     }  
  29.                 }  


這裏先獲取當前焦點控件,而後根據direction獲取下一個最佳的控件,獲取控件後調用他的requestFocus,並給前面的焦點控件一個機會處理失去焦點事件,看一下focusSearchthis

 

[java]  view plain  copy
 
  1. public View focusSearch(int direction) {  
  2.     if (mParent != null) {//父控件不爲空,調用它的focusSearch  
  3.         return mParent.focusSearch(this, direction);  
  4.     } else {  
  5.         return null;  
  6.     }  
  7. }  


一直調用parent的focusSearch,最終到spa

 

[java]  view plain  copy
 
  1. public View focusSearch(View focused, int direction) {  
  2.     if (isRootNamespace()) {//已是Root層 (installDecor  mDecor.setIsRootNamespace(true);)  
  3.         // root namespace means we should consider ourselves the top of the  
  4.         // tree for focus searching; otherwise we could be focus searching  
  5.         // into other tabs.  see LocalActivityManager and TabHost for more info  
  6.         return FocusFinder.getInstance().findNextFocus(this, focused, direction);//查找下一個可得到焦點的控件  
  7.     } else if (mParent != null) {//繼續調用父控件的focusSearch  
  8.         return mParent.focusSearch(focused, direction);  
  9.     }  
  10.     return null;  
  11. }  


若是已是根控件,調用FocusFinder的findNextFocus,最終調用它的findNextFocus.net

 

[java]  view plain  copy
 
  1. private View findNextFocus(ViewGroup root, View focused, Rect focusedRect, int direction) {  
  2.     View next = null;  
  3.     if (focused != null) {  
  4.         next = findNextUserSpecifiedFocus(root, focused, direction);//是xml裏經過android:nextFocusUp="..."等或者代碼特別指定的焦點順序  
  5.     }  
  6.     if (next != null) {//已經找到  
  7.         return next;  
  8.     }  
  9.     ArrayList<View> focusables = mTempList;//mTempList  
  10.     try {  
  11.         focusables.clear();  
  12.         root.addFocusables(focusables, direction);//獲取全部能夠獲取焦點的控件  
  13.         if (!focusables.isEmpty()) {  
  14.             next = findNextFocus(root, focused, focusedRect, direction, focusables);//查找下一個焦點控件  
  15.         }  
  16.     } finally {  
  17.         focusables.clear();  
  18.     }  
  19.     return next;  
  20. }  



先看一下該控件是否已經設置過它的焦點移動事件,indNextUserSpecifiedFocus就是幹這個事的,此方法先去判斷特定Id值是否存在,若存在則查詢出Id對應的view.其實這些Id就是xml裏經過android:nextFocusUp="..."等或者代碼特別指定的焦點順序.因此在此過程先判斷,若存在,說明下個焦點已經找到,直接返回.,未找到,則調用findNextFocus繼續查找rest

 

[java]  view plain  copy
 
  1. private View findNextFocus(ViewGroup root, View focused, Rect focusedRect,  
  2.         int direction, ArrayList<View> focusables) {  
  3.     if (focused != null) {  
  4.         if (focusedRect == null) {  
  5.             focusedRect = mFocusedRect;//焦點控件大小  
  6.         }  
  7.         // fill in interesting rect from focused  
  8.         focused.getFocusedRect(focusedRect);  
  9.         root.offsetDescendantRectToMyCoords(focused, focusedRect);  
  10.     } else {  
  11.         if (focusedRect == null) {  
  12.             focusedRect = mFocusedRect;  
  13.             // make up a rect at top left or bottom right of root  
  14.             switch (direction) {  
  15.                 case View.FOCUS_RIGHT:  
  16.                 case View.FOCUS_DOWN:  
  17.                     setFocusTopLeft(root, focusedRect);  
  18.                     break;  
  19.                 case View.FOCUS_FORWARD:  
  20.                     if (root.isLayoutRtl()) {  
  21.                         setFocusBottomRight(root, focusedRect);  
  22.                     } else {  
  23.                         setFocusTopLeft(root, focusedRect);  
  24.                     }  
  25.                     break;  
  26.   
  27.                 case View.FOCUS_LEFT:  
  28.                 case View.FOCUS_UP:  
  29.                     setFocusBottomRight(root, focusedRect);  
  30.                     break;  
  31.                 case View.FOCUS_BACKWARD:  
  32.                     if (root.isLayoutRtl()) {  
  33.                         setFocusTopLeft(root, focusedRect);  
  34.                     } else {  
  35.                         setFocusBottomRight(root, focusedRect);  
  36.                     break;  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     switch (direction) {  
  43.         case View.FOCUS_FORWARD:  
  44.         case View.FOCUS_BACKWARD:  
  45.             return findNextFocusInRelativeDirection(focusables, root, focused, focusedRect,  
  46.                     direction);  
  47.         case View.FOCUS_UP:  
  48.         case View.FOCUS_DOWN:  
  49.         case View.FOCUS_LEFT:  
  50.         case View.FOCUS_RIGHT:  
  51.             return findNextFocusInAbsoluteDirection(focusables, root, focused,//根據方向查找  
  52.                     focusedRect, direction);  
  53.         default:  
  54.             throw new IllegalArgumentException("Unknown direction: " + direction);  
  55.     }  
  56. }  


調用findNextFocusInAbsoluteDirection查找下一個焦點控件xml

 

[java]  view plain  copy
 
  1. View findNextFocusInAbsoluteDirection(ArrayList<View> focusables, ViewGroup root, View focused,  
  2.         Rect focusedRect, int direction) {//得到焦點控件的位置矩陣.而後經過比較獲得下一個焦點的控件  
  3.     // initialize the best candidate to something impossible  
  4.     // (so the first plausible view will become the best choice)  
  5.     mBestCandidateRect.set(focusedRect);//設置mBestCandidateRect  
  6.     switch(direction) {  
  7.         case View.FOCUS_LEFT:  
  8.             mBestCandidateRect.offset(focusedRect.width() + 1, 0);  
  9.             break;  
  10.         case View.FOCUS_RIGHT:  
  11.             mBestCandidateRect.offset(-(focusedRect.width() + 1), 0);  
  12.             break;  
  13.         case View.FOCUS_UP:  
  14.             mBestCandidateRect.offset(0, focusedRect.height() + 1);  
  15.             break;  
  16.         case View.FOCUS_DOWN:  
  17.             mBestCandidateRect.offset(0, -(focusedRect.height() + 1));  
  18.     }  
  19.   
  20.     View closest = null;  
  21.   
  22.     int numFocusables = focusables.size();  
  23.     for (int i = 0; i < numFocusables; i++) {//查找最佳的焦點控件  
  24.         View focusable = focusables.get(i);  
  25.   
  26.         // only interested in other non-root views  
  27.         if (focusable == focused || focusable == root) continue;  
  28.   
  29.         // get focus bounds of other view in same coordinate system  
  30.         focusable.getFocusedRect(mOtherRect);//獲取當其擁有焦點時的區域大小  
  31.         root.offsetDescendantRectToMyCoords(focusable, mOtherRect);  
  32.   
  33.         if (isBetterCandidate(direction, focusedRect, mOtherRect, mBestCandidateRect)) {//比較和Best哪一個更好  
  34.             mBestCandidateRect.set(mOtherRect);  
  35.             closest = focusable;//更合適  
  36.         }  
  37.     }  
  38.     return closest;//返回  
  39. }  


根據焦點控件的區域去查找一個合適的,具體查找,比較那個合適比較複雜,暫時還沒看懂。blog

關於移動的時候有時候沒有焦點,咱們能夠查看在findNextFocus(ViewGroup root, View focused, Rect focusedRect, int direction)這個函數中,root.addFocusables(focusables, direction);

相關文章
相關標籤/搜索