`
liuguofeng
  • 浏览: 450095 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android ViewGroup.setDescendantFocusability函数

 
阅读更多

这个函数是在ViewGroup里定义的,主要用于控制child View获取焦点的能力,比如是否阻止child View获取焦点。

 

他有三个常量可供设置

 

  1. FOCUS_BEFORE_DESCENDANTS ViewGroup本身先对焦点进行处理,如果没有处理则分发给child View进行处理
  2. FOCUS_AFTER_DESCENDANTS 先分发给Child View进行处理,如果所有的Child View都没有处理,则自己再处理
  3. FOCUS_BLOCK_DESCENDANTS ViewGroup本身进行处理,不管是否处理成功,都不会分发给ChildView进行处理
 
我们看下这个方法的实现
Setdescendantfocusability(int focusability)代码  收藏代码
  1. public void setDescendantFocusability(int focusability) {  
  2.         switch (focusability) {  
  3.             case FOCUS_BEFORE_DESCENDANTS:  
  4.             case FOCUS_AFTER_DESCENDANTS:  
  5.             case FOCUS_BLOCK_DESCENDANTS:  
  6.                 break;  
  7.             default:  
  8.                 throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "  
  9.                         + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");  
  10.         }  
  11.         mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;  
  12.         mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);  
  13.     }  
 

 

可以看到,只有这三个常量可以设置,不是这三个常量会抛出异常的。

 

 

设置后,会在requestFocus(int direction, Rect previouslyFocusedRect) 方法里根据设置进行相应的处理。来看下实现

 

Requestfocus(int direction, rect previouslyfocusedrect)代码  收藏代码
  1. public boolean requestFocus(int direction, Rect previouslyFocusedRect) {  
  2.         if (DBG) {  
  3.             System.out.println(this + " ViewGroup.requestFocus direction="  
  4.                     + direction);  
  5.         }  
  6.         int descendantFocusability = getDescendantFocusability();  
  7.   
  8.         switch (descendantFocusability) {  
  9.             case FOCUS_BLOCK_DESCENDANTS:  
  10.                 return super.requestFocus(direction, previouslyFocusedRect);  
  11.             case FOCUS_BEFORE_DESCENDANTS: {  
  12.                 final boolean took = super.requestFocus(direction, previouslyFocusedRect);  
  13.                 return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);  
  14.             }  
  15.             case FOCUS_AFTER_DESCENDANTS: {  
  16.                 final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);  
  17.                 return took ? took : super.requestFocus(direction, previouslyFocusedRect);  
  18.             }  
  19.             default:  
  20.                 throw new IllegalStateException("descendant focusability must be "  
  21.                         + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "  
  22.                         + "but is " + descendantFocusability);  
  23.         }  
  24.     }  

 

通过这里的实现可以看到上面定义的三个常量设置的意思。。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics