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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With