Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused with removeAllViewsInLayout(), postInvalidate() and refreshDrawableState()

Tags:

android

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?

like image 700
SPG Avatar asked Nov 24 '11 01:11

SPG


1 Answers

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.

like image 134
Ted Hopp Avatar answered Oct 16 '22 06:10

Ted Hopp