关于View.onScrollChanged()的来源

时间:2013-03-14 09:56:40

标签: java android view android-source

以下是protected void View.onScrollChanged(int l, int t, int oldl, int oldt)的来源:

/**
 * This is called in response to an internal scroll in this view (i.e., the
 * view scrolled its own contents). This is typically as a result of
 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
 * called.
 *
 * @param l Current horizontal scroll origin.
 * @param t Current vertical scroll origin.
 * @param oldl Previous horizontal scroll origin.
 * @param oldt Previous vertical scroll origin.
 */
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    if (AccessibilityManager.getInstance(mContext).isEnabled()) {
        postSendViewScrolledAccessibilityEventCallback();
    }

    mBackgroundSizeChanged = true;

    final AttachInfo ai = mAttachInfo;
    if (ai != null) {
        ai.mViewScrollChanged = true;
    }
}

问题在于这一行:final AttachInfo ai = mAttachInfo;。出于什么目的ai被引入以及为什么要final

1 个答案:

答案 0 :(得分:3)

最有可能的是,mAttachInfovolatile;那么ai的目的是避免NullPOinterException当两个线程访问此对象并且在检查mAttachInfo之后但在执行mAttachInfo!=null之前写入mAttachInfo.mViewScrollChanged = true;