setDescendantFocusability

努力学习.png

1 简介

setDescendantFocusability(int focusability)ViewGroup 的方法,其作用是设置 ViewGroup 的子View获取焦点的能力。

科普一下英文单词

descendant 
英[dɪˈsendənt]  美[dɪˈsɛndənt]  复数:[descendants] 

n. 后裔; 后代; (由过去类似物发展来的) 派生物; 弟子; 
adj.下降的; 祖传的; |

[例句]They are descendants of the original English and Scottish settlers.
他们是最初的英格兰和苏格兰定居者的后代。

2 实现

setDescendantFocusability(int focusability) 的具体实现代码如下所示:

/**
   * Set the descendant focusability of this view group. This defines the relationship
   * between this view group and its descendants when looking for a view to
   * take focus in {@link #requestFocus(int, android.graphics.Rect)}.
   *
   * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS},
   *   {@link #FOCUS_BLOCK_DESCENDANTS}.
   */
  public void setDescendantFocusability(int focusability) {
      switch (focusability) {
          case FOCUS_BEFORE_DESCENDANTS:
          case FOCUS_AFTER_DESCENDANTS:
          case FOCUS_BLOCK_DESCENDANTS:
              break;
          default:
              throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, "
                      + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS");
      }
      mGroupFlags &= ~FLAG_MASK_FOCUSABILITY;
      mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY);
  } 

setDescendantFocusability(int focusability) 方法可以传入的参数只有三种,
分别为:FOCUS_BEFORE_DESCENDANTSFOCUS_AFTER_DESCENDANTSFOCUS_BLOCK_DESCENDANTS ,从源码中也能看出来,除了这三种参数,传入其它参数会抛出非法参数异常。

FOCUS_BEFORE_DESCENDANTS

该参数表明 ViewGroup 自身先处理焦点,如果没有处理则分发给子View进行处理。

FOCUS_AFTER_DESCENDANTS

该参数表明子View优先处理焦点,如果所有的子View都没有处理,则 ViewGroup 自身再处理。

FOCUS_BLOCK_DESCENDANTS

该参数表明 ViewGroup 自身处理焦点,不会分发给子View处理。
而这三种参数实际上是在 requestFocus(int direction, Rect previouslyFocusedRect) 方法中起作用的,如下源码所示。

    /**
     * {@inheritDoc}
     *
     * Looks for a view to give focus to respecting the setting specified by
     * {@link #getDescendantFocusability()}.
     *
     * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to
     * find focus within the children of this group when appropriate.
     *
     * @see #FOCUS_BEFORE_DESCENDANTS
     * @see #FOCUS_AFTER_DESCENDANTS
     * @see #FOCUS_BLOCK_DESCENDANTS
     * @see #onRequestFocusInDescendants(int, android.graphics.Rect) 
     */
    @Override
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        if (DBG) {
            System.out.println(this + " ViewGroup.requestFocus direction="
                    + direction);
        }
        int descendantFocusability = getDescendantFocusability();

        switch (descendantFocusability) {
            case FOCUS_BLOCK_DESCENDANTS:
                return super.requestFocus(direction, previouslyFocusedRect);
            case FOCUS_BEFORE_DESCENDANTS: {
                final boolean took = super.requestFocus(direction, previouslyFocusedRect);
                return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
            }
            case FOCUS_AFTER_DESCENDANTS: {
                final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
                return took ? took : super.requestFocus(direction, previouslyFocusedRect);
            }
            default:
                throw new IllegalStateException("descendant focusability must be "
                        + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
                        + "but is " + descendantFocusability);
        }
    }

如果想让 RecyclerView 的子View优先获取焦点的话,就可以在布局中添加下面这一行代码。

android:descendantFocusability="afterDescendants"

或者在Java代码中添加下面这一行代码。

mRecyclerView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文重点针对android TV开发的同学,分析遥控或键盘按键事件后焦点的分发机制。尤其是刚从手机开发转向TV开发...
    wanderingGuy阅读 7,555评论 0 5
  • 本来之前说view下篇是写onMeasure,onLayou,onDraw相关的,笔者做盒子开发,遥控器按键,碰到...
    passiontim阅读 11,589评论 0 4
  • 这两个月提不起精神去跑步,年初给自己定的锻炼目标基本是落空了,虽然时不时鄙视下自己,但鄙视多了也就习惯了,跑得次数...
    兮兮0225阅读 1,508评论 0 0
  • 阿华在高二的时候,突然情窦初开,瞄上了我们班的班花孙倩。 孙倩个子苗条,皮肤白皙,留着秀丽长发,最妙的是她的眼睛,...
    公子安然阅读 4,260评论 0 4
  • 师为国医贤妙手仁德重师生忙碌不得闲心系患者情 酸苦甘辛咸苦乐不由衷阴阳八卦定乾坤共筑岐黄梦 2017年6月24日,...
    古典悠梦阅读 3,375评论 0 3

友情链接更多精彩内容