与removeAllViewsInLayout()、postInvalidate()和refreshDrawableState()混淆
我真的对这三个函数感到困惑: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您调用
postInvalidate()
(或者在 UI 线程上仅调用invalidate()
),系统将安排视图的重绘。然后,您的onDraw()
方法就可以执行它想要的任何操作,包括仅绘制空白画布。如果您调用
removeAllViewsInLayout()
,它将执行此操作 - 删除每个视图,包括您的自定义视图。你可能不想这样做。 (编辑:removeAllViewsInLayout()
旨在仅作为ViewGroup
布局计算的一部分进行调用。典型用途是由ViewGroup
它“拥有”大量视图,但在onLayout()
处理过程中决定仅显示那些实际适合屏幕的子视图。如果您没有在计算视图布局,则应该这样做。称呼 。仅当您的视图使用对状态敏感的
Drawable
对象时,调用refreshDrawableState()
才有用 看法。例如,按钮将使用它,以便在按下按钮时背景可绘制对象改变颜色。对于您正在做的事情,您也不需要费心使用此方法。If you call
postInvalidate()
(or just callinvalidate()
if on the UI thread), the system will schedule a redraw of your view. YouronDraw()
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 aViewGroup
. A typical use would be by aViewGroup
that "owns" a large number of views but duringonLayout()
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 callremoveAllViews()
instead.)Calling
refreshDrawableState()
is only useful if your view is using aDrawable
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.