Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's difference between removeAllViews() and removeAllViewsInLayout()

I am populating a linear layout dynamically. Depending upon response, I have to clear the past child views and create new views. I have read the document, but still be confused with the couple methods, they all look the same function. Which function I should use.

like image 477
L_YQing Avatar asked Oct 12 '22 09:10

L_YQing


1 Answers

As Scott Biggs points out, the difference is not a big one. The only difference is that removeAllViews() calls requestLayout() and invalidate() afterwards. The key to why this difference is here is to understand the naming of removeAllViewInLayout(). Confusingly, its meaning isn't "remove all views within this view layout."

If we look at the similar method, removeViewInLayout(), we can understand what it's supposed to mean:

Removes a view during layout. This is useful if in your onLayout() method, you need to remove more views.

So removeAllViewsInLayout() actually means "remove all views, and we're calling this method during a layout pass (i.e. onLayout())". That's why removeAllViewsInLayout() doesn't call through to requestLayout(), as it's assumed that you're already currently inside a layout pass, so requesting another layout pass is unneeded.

If you use removeAllViewsInLayout(), then it's your responsibility to ensure that you're calling this during a layout pass, or to properly call requestLayout() and invalidate() as needed.

like image 37
David Liu Avatar answered Oct 13 '22 23:10

David Liu