Android8.0 焦点处理流程(一)

Android8.0 焦点处理流程(一)
Android8.0 焦点处理流程(二)

文章目录

    • 前言
    • 术语
    • View树中焦点
      • 获取焦点的前提
      • View
        • 请求焦点流程
        • 清除焦点
      • ViewGroup
        • 请求焦点
        • 清除焦点
    • 总结

前言

现在基本上都是触摸屏手机,导致很多开发者对焦点并没有深刻认识,但仍然存在一些非触屏手机,还有Android TV平台都与焦点息息相关,这里就总结下自己看过多篇文章和自己的实践经验。先看两张焦点图。
Android8.0 焦点处理流程(一)_第1张图片

Android8.0 焦点处理流程(一)_第2张图片

焦点基础及源码分析通过两篇来带大家深入认识。第一篇从最基本的View,ViewGroup获取焦点说起。

术语

  • 父类 extends后的类叫做父类
  • 父View View树中的父级View称作父View
  • 拥有焦点 表示该View获取到了焦点,focused状态为true
  • 包含焦点 表示该View或者其子View获取到了焦点,而自己focused状态有可能为false,但一定有一个子View focused为true。

View树中焦点

先看下Activity的View树结构
Android8.0 焦点处理流程(一)_第3张图片
其顶层就是DecorView,子View可以是View也可以是ViewGroup,但父View一定是ViewGroup.

获取焦点的前提

  1. View#isFocusable返回true, 如果在触摸模式, 则View#isFocusableInTouchMode也要返回true
  2. View及父View必须处于可见
  3. 父View不能有FOCUS_BLOCK_DESCENDANTS属性

View

请求焦点流程

调用View#requestFocus方法
View.java

public final boolean requestFocus() {
    return requestFocus(View.FOCUS_DOWN);
}
private boolean requestFocusNoSearch(int direction, Rect previouslyFocusedRect) {
    // need to be focusable
    // 1. 判断View是否可获取焦点和是否可见
    if ((mViewFlags & FOCUSABLE) != FOCUSABLE
            || (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
        return false;
    }
    // 2. 如果为触摸模式下,则FOCUSABLE_IN_TOUCH_MODE必须为true才可以去获得焦点
    // need to be focusable in touch mode if in touch mode
    if (isInTouchMode() &&
        (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
           return false;
    }
    // 3. 判断所有父View是否有BLOCK子类焦点的属性,如果没有才可以去获取焦点
    // need to not have any parents blocking us
    if (hasAncestorThatBlocksDescendantFocus()) {
        return false;
    }
    // 4 下文分析
    handleFocusGainInternal(direction, previouslyFocusedRect);
    return true;
}

注释1,2,3就是上文提到的获取焦点的3个前提
接下来分析注释4

void handleFocusGainInternal(@FocusRealDirection int direction, Rect previouslyFocusedRect) {
    // 1. 置FOCUSED标记位
    if ((mPrivateFlags & PFLAG_FOCUSED) == 0) {
        mPrivateFlags |= PFLAG_FOCUSED;
        // 调用RootView 
        View oldFocus = (mAttachInfo != null) ? getRootView().findFocus() : null;

        if (mParent != null) {
            // 2. 告知父View自己已聚焦,下文详细分析
            mParent.requestChildFocus(this, this);
            updateFocusedInCluster(oldFocus, direction);
        }
        if (mAttachInfo != null) {
            // 3. 通知OnGlobalFocusChangeListener
            mAttachInfo.mTreeObserver.dispatchOnGlobalFocusChange(oldFocus, this);
        }
        // 4. 触发OnFocusChangeListener回调,只能受到自己View焦点状态的改变
        onFocusChanged(true, direction, previouslyFocusedRect);
        ...
    }
}

继续分析注释2的requestChildFocus,该方法作用通知父View自己已获取焦点,然后父View去更新自己管辖范围(直接子View)内焦点状态,将状态继续通知给其父View

ViewGroup.java

@Override
public void requestChildFocus(View child, View focused) {
    if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) {
        return;
    }

    // 1. 清除自己的焦点,如果有的话
    // Unfocus us, if necessary
    super.unFocus(focused);

    // We had a previous notion of who had focus. Clear it.
    // 2. mFocused用指向自己直接子View拥有或者包含焦点的View,如果存在则让其失焦
    if (mFocused != child) {
        if (mFocused != null) {
            mFocused.unFocus(focused);
        }
        // 3. mFocused重新指向新的请求焦点的View,
        mFocused = child;
    }
    if (mParent != null) {
        // 4. 继续通知父View自己已有或者已拥有焦点
        mParent.requestChildFocus(this, focused);
    }
}

这里解释下mFocused是什么,直接看下图

mFocused标识了自己的直接子View的焦点状态。

mParent.requestChildFocus方法一级一级向上调用通知自己焦点状态直到顶层View,对于Activity,顶层View就是DecorView,然后继续调用到所有View的管理类ViewRootImpl的requestChildFocus的方法,其进行整个界面重绘。

ViewRoolImpl.java

@Override
public void requestChildFocus(View child, View focused) {
    checkThread();
    // 会执行performTraversals
    scheduleTraversals();
}
// 界面重绘
private void performTraversals() {
    ...
    performMeasure(); // 回调onMeasue()
    ...
    performLayout(); // 回调onLayout()
    ...
    performDraw(); // 回调ondraw()
    ...
}

清除焦点

View#clearFocus主动放弃焦点
View.java

public void clearFocus() {
    clearFocusInternal(null, true, true);
}

void clearFocusInternal(View focused, boolean propagate, boolean refocus) {        
    // 1. 改变焦点标志位
    if ((mPrivateFlags & PFLAG_FOCUSED) != 0) {
        mPrivateFlags &= ~PFLAG_FOCUSED;
        if (propagate && mParent != null) {
        // 2. 通知父view当前View放弃焦点,和requestChildFocus相反
            mParent.clearChildFocus(this);
        }
        // 3. 触发OnFocusChangeListener回调
        onFocusChanged(false, 0, null);
        refreshDrawableState();
        // 4. rootViewRequestFocus()调用当前View树的根View(rootView)的requestFocus方法
        if (propagate && (!refocus || !rootViewRequestFocus())) {
            notifyGlobalFocusCleared(this);
        }
    }
}

ViewGroup

请求焦点

也是调用requestFocus()方法

ViewGroup.java

@Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
    int descendantFocusability = getDescendantFocusability();
    switch (descendantFocusability) {
        // 1. 如果BLOCK子View焦点,则直接走当前View请求焦点的流程
        case FOCUS_BLOCK_DESCENDANTS:
            return super.requestFocus(direction, previouslyFocusedRect);
        case FOCUS_BEFORE_DESCENDANTS: {
            // 2. 如果为BEFORE子View,则先走View请求焦点流程,如果没有得到焦点则走下个方法onRequestFocusInDescendants方法,其根据direction查找所有子View去请求焦点
            final boolean took = super.requestFocus(direction, previouslyFocusedRect);
            return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect);
        }
        case FOCUS_AFTER_DESCENDANTS: {
            // 3. AFTER子View,和BEFORE相反,先遍历所有子View请求焦点,如果没有请求到,则走当前View请求焦点流程
            final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect);
            return took ? took : super.requestFocus(direction, previouslyFocusedRect);
        }
    }
}

清除焦点

ViewGroup.java

@Override
public void clearFocus() {
    if (mFocused == null) {
        // mFocused为null,则子View无焦点,则清除自己焦点
        super.clearFocus();
    } else {
        // mFocused置为null,并使其清除焦点
        View focused = mFocused;
        mFocused = null;
        focused.clearFocus();
    }
}

总结

界面是由View树组成,父View通过mFocused来标识直接子View的焦点状态,当子View焦点状态改变会通知其父View,如果该ViewGroup之前有拥有焦点的View,父View会清除其焦点,并更新mFocused为新View,然后继续将自己状态通知其父View,这样一级一级更新,整个View树焦点状态保持最新,最后由ViewRootImpl更新界面

按D-pad(遥控器上下左右就是)键盘,焦点如何移动,源码分析尽在下篇:)Android8.0 焦点处理流程(二)

你可能感兴趣的:(Android开发)