Android removeAllViews() 和 removeAllViewsInLayout() 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11952598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's difference between removeAllViews() and removeAllViewsInLayout()
提问by L_YQing
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.
我正在动态填充线性布局。根据响应,我必须清除过去的子视图并创建新视图。我已经阅读了文档,但仍然对这两种方法感到困惑,它们看起来都具有相同的功能。我应该使用哪个功能。
回答by David Liu
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."
正如 Scott Biggs 所指出的,差异并不大。唯一的区别是removeAllViews()
调用requestLayout()
和invalidate()
之后。之所以出现这种差异的关键是了解removeAllViewInLayout()
. 令人困惑的是,它的意思不是“删除此视图布局中的所有视图”。
如果我们查看类似的方法 removeViewInLayout(),我们就可以理解它的含义:
Removes a view during layout. This is useful if in your onLayout() method, you need to remove more views.
在布局期间删除视图。如果在您的 onLayout() 方法中,您需要删除更多视图,这将非常有用。
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.
所以removeAllViewsInLayout()
实际上意味着“删除所有视图,我们在布局传递期间调用此方法(即 onLayout())”。这就是为什么removeAllViewsInLayout()
不调用 to requestLayout()
,因为它假定您当前已经在布局传递中,因此不需要请求另一个布局传递。
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.
如果您使用的removeAllViewsInLayout()
,那么它是你的责任,以确保您正在布局传递过程中调用这个,或者正确地调用requestLayout()
和invalidate()
按需要。
回答by Alexandr
removeAllViews() : -Call this method to remove all child views from the ViewGroup. removeAllViewsInLayout() : -Called by a ViewGroup subclass to remove child views from itself, when it must first know its size on screen before it can calculate how many child views it will render.
removeAllViews() : - 调用此方法从 ViewGroup 中删除所有子视图。removeAllViewsInLayout() : - 由 ViewGroup 子类调用以从自身删除子视图,当它必须首先知道其在屏幕上的大小才能计算将呈现多少子视图时。
Cheers!
干杯!
回答by Scott Biggs
Well, looking at the source, there isn't much difference:
嗯,看源码,没有太大区别:
public void removeAllViews() {
removeAllViewsInLayout(); // Details implemented here
requestLayout();
invalidate(true);
}
So unless you want to call invalidate()
at a time of your choosing, you might as well use removeAllViews()
and save yourself a bit of typing.
因此,除非您想invalidate()
在您选择的时间打电话,否则您最好使用removeAllViews()
并节省一些打字时间。
EDIT
For a more detailed explanation, see David Lui's answer. To sum it up, use removeAllViews()
unless you're in the process of constructing a View--in which case you'd call removeAllViewsInLayout()
.
编辑
有关更详细的解释,请参阅David Lui的回答。总而言之,removeAllViews()
除非您正在构建视图,否则请使用- 在这种情况下,您将调用removeAllViewsInLayout()
.