与removeAllViewsInLayout()、postInvalidate()和refreshDrawableState()混淆

发布于 2024-12-17 21:00:43 字数 482 浏览 0 评论 0原文

我真的对这三个函数感到困惑:removeAllViewsInLayout()postInvalidate()refreshDrawableState()

removeAllViewsInLayout()

当我在程序中使用 removeAllViewsInLayout() 时,所有视图都消失了。但是当触发 postInvalidate() 刷新时,什么也没有。我认为 removeAllViewsInLayout() 删除了我的所有视图。有没有办法清除我视图中的所有内容但不删除它?

postInvalidate()

我想刷新一下我的看法。但使用 refreshDrawableState() 时,我只能执行一次,为什么?

I am really confused with this three functions: removeAllViewsInLayout(), postInvalidate() and refreshDrawableState().

removeAllViewsInLayout()

When I use removeAllViewsInLayout() in my program, all views were gone. But when trigger postInvalidate() to refresh, nothing. I think removeAllViewsInLayout() deletes all of my views. Is there a way to clear all things in my view but not delete it?

postInvalidate()

I want to refresh my view. But with refreshDrawableState(), I can do only once, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-12-24 21:00:43

如果您调用 postInvalidate()(或者在 UI 线程上仅调用 invalidate()),系统将安排视图的重绘。然后,您的 onDraw() 方法就可以执行它想要的任何操作,包括仅绘制空白画布。

如果您调用removeAllViewsInLayout(),它将执行此操作 - 删除每个视图,包括您的自定义视图。你可能不想这样做。 (编辑:removeAllViewsInLayout() 旨在仅作为 ViewGroup 布局计算的一部分进行调用。典型用途是由 ViewGroup它“拥有”大量视图,但在 onLayout() 处理过程中决定仅显示那些实际适合屏幕的子视图。如果您没有在计算视图布局,则应该这样做。称呼 。

仅当您的视图使用对状态敏感的 Drawable 对象时,调用 refreshDrawableState() 才有用 看法。例如,按钮将使用它,以便在按下按钮时背景可绘制对象改变颜色。对于您正在做的事情,您也不需要费心使用此方法。

If you call postInvalidate() (or just call invalidate() if on the UI thread), the system will schedule a redraw of your view. Your onDraw() method can then do whatever it wants, including just drawing a blank canvas.

If you call removeAllViewsInLayout() it will do just that -- remove every view, including your custom view. You probably don't want to do that. (EDIT: removeAllViewsInLayout() is intended to be called only as part of layout calculations for a ViewGroup. A typical use would be by a ViewGroup that "owns" a large number of views but during onLayout() processing decides to display only those children that actually fits on the screen. If you are not in the process of computing the view layout, you should call removeAllViews() instead.)

Calling refreshDrawableState() is only useful if your view is using a Drawable object that is sensitive to the state of the view. For example, a Button will use this so that the background drawable changes color when the button is pressed. For what you are doing, you don't need to bother with this method, either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文