简体   繁体   English

如何清除度量缓存android

[英]How to clear the measure cache android

I have an app that lays out controls on the screen at runtime, it passes through each control calling measure on a View with a provided width and WRAP_CONTENT as height.我有一个应用程序,它在运行时在屏幕上WRAP_CONTENT控件,它通过视图上的每个控件调用measure ,并使用提供的宽度和WRAP_CONTENT作为高度。

It does this before any data is set on the view to show a stencil version of the view and then after actual data is set on the view.它在视图上设置任何数据以显示视图的模板版本之前执行此操作,然后在视图上设置实际数据之后执行此操作。

The issue is that since it calls measure twice with same input, the second time we hit the measure cache and it doesn't re-measure even though the actual measurements of the view have changed since data on the controls have changed.问题是,由于它使用相同的输入调用了两次 measure,第二次我们访问了 measure 缓存,即使视图的实际测量值已经更改,因为控件上的数据发生了变化,它也不会重新测量。

Is there any way to force it to re-measure on the second call?有没有办法强制它在第二次调用时重新测量?

You can use View.forceLayout to invalidate the cache and force a full layout pass.您可以使用View.forceLayout使缓存无效并强制执行完整的布局传递。
The source code shows what it does:源代码显示了它的作用:

/**
 * Forces this view to be laid out during the next layout pass.
 * This method does not call requestLayout() or forceLayout()
 * on the parent.
 */
public void forceLayout() {
    if (mMeasureCache != null) mMeasureCache.clear();

    mPrivateFlags |= PFLAG_FORCE_LAYOUT;
    mPrivateFlags |= PFLAG_INVALIDATED;
}

I haven't found a way to clear the cache, but if one of the values is UNSPECIFIED, it looks like a cache mismatch can be forced similar to this:我还没有找到清除缓存的方法,但是如果其中一个值是 UNSPECIFIED,看起来可以像这样强制缓存不匹配:

int fakeSpace = (int) (Math.random() * 9999999);
int spec = View.MeasureSpec.makeMeasureSpec(fakeSpace, View.MeasureSpec.UNSPECIFIED);
view.measure(spec, spec);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM