Android:requestLayout、invalidate 和 postInvalidate 的区别

提醒:下面源码来自SDK里Android-34版本

一、requestLayout

点击查看requestLayout官网文档

1.1 requestLayout方法源码
 /**
     * Call this when something has changed which has invalidated the
     * layout of this view. This will schedule a layout pass of the view
     * tree. This should not be called while the view hierarchy is currently in a layout
     * pass ({
   
   @link #isInLayout()}. If layout is happening, the request may be honored at the
     * end of the current layout pass (and then layout will run again) or after the current
     * frame is drawn and the next layout occurs.
     *
     * <p>Subclasses which override this method should call the superclass method to
     * handle possible request-during-layout errors correctly.</p>
     * 上面翻译
     * 当某些东西发生改变使。无效时调用这个
     * 这个视图的布局。这将安排视图的布局传递树。
     * 当视图层次结构当前处于布局时,不应该调用这个函数
     * pass ({
   
   @link #isInLayout()})。如果正在进行布局,
     * 请求可能会在结束当前布局传递(然后布局将再次运行)
     * 或在当前绘制框架,并发生下一个布局。
     * 
     * 重写此方法的子类应该调用父类方法正确处理布局期间可能出现的请求错误。
     */
    @CallSuper
    public void requestLayout() {
   
   
        if (isRelayoutTracingEnabled()) {
   
   
            Trace.instantForTrack(TRACE_TAG_APP, "requestLayoutTracing",
                    mTracingStrings.classSimpleName);
            printStackStrace(mTracingStrings.requestLayoutStacktracePrefix);
        }

        if (mMeasureCache != null) mMeasureCache.clear();

        if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == null) {
   
   
            // Only trigger request-during-layout logic if this is the view requesting it,
            // not the views in its parent hierarchy
            ViewRootImpl viewRoot = getViewRootImpl();
            if (viewRoot != null && viewRoot.isInLayout()) {
   
   
                if (!viewRoot.requestLayoutDuringLayout(this)) {
   
   
                    return;
                }
            }
            mAttachInfo.mViewRequestingLayout = this;
        }

        mPrivateFlags |= PFLAG_FORCE_LAYOUT;
        mPrivateFlags |= PFLAG_INVALIDATED;

        if (mParent != null && !mParent.isLayoutRequested()) {
   
   
            mParent.requestLayout();
        }
        if (mAttachInfo != null && mAttachInfo.mViewRequestingLayout == this) {
   
   
            mAttachInfo.mViewRequestingLayout = null;
        }
    }

在这里插入图片描述

1.2 requestLayoutDuringLayout方法源码
 /**
     * Called by {
   
   @link android.view.View#requestLayout()} if the view hierarchy is currently
     * undergoing a layout pass. requestLayout(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值