android 焦点获取流程(转)

结论:在ViewGroup中我们可以重新实现addFocusables,已统一处理判断View是否可以获取到焦点.

android一般都是手机或者平板,一般都是点击的时候获取焦点,当我们添加遥控或手柄支持焦点移动时,这个时候焦点的查找就比较明显了,那么Android的焦点是怎么查找的呢。

我们从handleImeFinishedEvent(ViewRootImpl.java)开始了解焦点的查找流程,handleImeFinishedEven是由dispatchImeFinishedEvent触发,dispatchImeFinishedEvent又是由InputMethodManager触发来的,

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,并给前面的焦点控件一个机会处理失去焦点事件,看一下focusSearch

[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,最终到

[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

[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继续查找

[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查找下一个焦点控件

[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. }  


根据焦点控件的区域去查找一个合适的,具体查找,比较那个合适比较复杂,暂时还没看懂。

关于移动的时候有时候没有焦点,我们可以查看在findNextFocus(ViewGroup root, View focused, Rect focusedRect, int direction)这个函数中,root.addFocusables(focusables, direction);

原文地址:https://www.cnblogs.com/jianglijs/p/8214233.html