深入理解:View和ViewGroup如何才能获取焦点

简介

一般我们知道View请求获取焦点调用requestFocus方法

查找流程

我们看看requestFocus方法

public final boolean requestFocus() {
        return requestFocus(View.FOCUS_DOWN);
    }

requestFocus中调用了requestFocus(View.FOCUS_DOWN) 我们接着看

public final boolean requestFocus(int direction) {
        return requestFocus(direction, null);
    }

又调用了 requestFocus(direction, null) 我们接着看

public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        return requestFocusNoSearch(direction, previouslyFocusedRect);
    }

注意:ViewGroup重写了requestFocus(direction, null)此方法,在里面处理焦点先给子View还是先给自己处理

又调用了requestFocusNoSearch(direction, previouslyFocusedRect) 着看

private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
        // need to be focusable
        if (!canTakeFocus()) { //判断是否具可以获取焦点
            return false;
        }

        // need to be focusable in touch mode if in touch mode
        if (isInTouchMode() &&
            (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
               return false;
        }

        // need to not have any parents blocking us
        if (hasAncestorThatBlocksDescendantFocus()) {
            return false;
        }

        if (!isLayoutValid()) {
            mPrivateFlags |= PFLAG_WANTS_FOCUS;
        } else {
            clearParentsWantFocus();
        }

        handleFocusGainInternal(direction, previouslyFocusedRect);
        return true;
    }

看到这个方法我们发现了一个方法 canTakeFocus 这个方法就是判断View是否可以聚焦,不可聚焦直接返回false

private boolean canTakeFocus() {
        return ((mViewFlags & VISIBILITY_MASK) == VISIBLE) //View可见
                && ((mViewFlags & FOCUSABLE) == FOCUSABLE)//Viewfocuable
                && ((mViewFlags & ENABLED_MASK) == ENABLED)//View enable
                && (sCanFocusZeroSized || !isLayoutValid() || hasSize()); 
                //sCanFocusZeroSized 这个值在Android P以下默认就是true,也就是在Android P以下一个View没有大小也是可聚焦的;
    }

看到这儿我们就明白了view获取焦点的条件是:

可见性为 VISIBLE 并且 focusable并且状态 enabled 并且有尺寸(有尺寸不管在哪个版本都是可聚焦的)。

ViewGroup的重写的方法

ViewGroup重写了 requestFocusNoSearch(direction, previouslyFocusedRect) 方法,看下

 @Override
    public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
        if (DBG) {
            System.out.println(this + " ViewGroup.requestFocus direction="
                    + direction);
        }
        int descendantFocusability = getDescendantFocusability(); //获取descendantFocusability 的值

        boolean result;
        switch (descendantFocusability) {
            case FOCUS_BLOCK_DESCENDANTS://是BLOCK则调用父类的方法,直接进入自己的聚焦流程
                result = super.requestFocus(direction, previouslyFocusedRect);
                break;
            case FOCUS_BEFORE_DESCENDANTS: {//如果是before则先自己尝试聚焦,如果自己不能聚焦则去查找子View是否可以聚焦
                final boolean took = super.requestFocus(direction, previouslyFocusedRect);
                result = took ? took : onRequestFocusInDescendants(direction,
                        previouslyFocusedRect);
                break;
            }
            case FOCUS_AFTER_DESCENDANTS: {//如果是AFTER 先给子View去聚焦,子View没有处理焦点则自己去请求聚焦
                final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
                result = took ? took : super.requestFocus(direction, previouslyFocusedRect);
                break;
            }
            default:
                throw new IllegalStateException("descendant focusability must be "
                        + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS "
                        + "but is " + descendantFocusability);
        }
        if (result && !isLayoutValid() && ((mPrivateFlags & PFLAG_WANTS_FOCUS) == 0)) {
            mPrivateFlags |= PFLAG_WANTS_FOCUS;
        }
        return result;
    }

首先获取descendantFocusability 的值

  • 如果FOCUS_BLOCK_DESCENDANTS则调用父类的方法,直接进入自己的聚焦流程,不给子View处理焦点的机会
  • 如果是FOCUS_BEFORE_DESCENDANTS则先自己尝试聚焦,如果自己不能聚焦则去查找子View聚焦
  • 如果是FOCUS_AFTER_DESCENDANTS先给子View去聚焦,子View没有处理焦点则自己去请求聚焦

descendantFocusability 决定了ViewGroup和子View请求焦点的先后顺序

descendantFocusability
xml属性  android:descendantFocusability=""
方法:setDescendantFocusability(int focusability)
取值:FOCUS_BEFORE_DESCENDANTS或者FOCUS_AFTER_DESCENDANTS或者FOCUS_BLOCK_DESCENDANTS

ViewGroup中 initViewGroup方法设置了默认值 FOCUS_BEFORE_DESCENDANTS

结论

  • 对于ViewGroup,我们调用ViewGroup的requestFocus方法时
    • 如果是FOCUS_BLOCK_DESCENDANTS,ViewGroup获取焦点和子View是一样的
    • 非FOCUS_BLOCK_DESCENDANTS的情况,焦点可能是ViewGroup获取到也可能是子View获取到

ViewGroup中descendantFocusability 决定了子View和ViewGroup获取焦点的先后顺序

无论是View还是ViewGroup,这个对象本身获取焦点:
必须同时满足三个条件:可见性Visible、focusable为true、状态为enable
对应代码就是setFocusable(true),setVisbility(View.Visible)、setEnable(true)。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值