gpt4 book ai didi

java - 如果我将 LinearLayout 实例化为现有 LinearLayout 的副本,那么一旦我对原始 LinearLayout 执行了一次操作,它也会在副本上完成

转载 作者:行者123 更新时间:2023-12-01 23:38:18 28 4
gpt4 key购买 nike

我有一个 LinearLayout,上面有几个 View 。所以我想要实现的是复制这个 LinearLayout 并在某个时候使用它。但是,当我对原始 LinearLayout 执行更改(例如方法 removeAllViews())时,它也会在副本上完成。

示例:

Log.d("debug1","1 vc"+variableContent.getChildCount());
this.anticsModulsVisibles = variableContent; //copy of the linearLayout
Log.d("debug1","2 vc"+variableContent.getChildCount());
Log.d("debug1","2.1 amv"+anticsModulsVisibles.getChildCount());
variableContent.removeAllViews(); //i remove the content of the original
Log.d("debug1","3 vc"+variableContent.getChildCount());
Log.d("debug1","4 amv"+anticsModulsVisibles.getChildCount());

以及日志的输出:

1 vc2

2 vc2

2.1 amv2

3 vc0

4 amv0

如您所见,anticsModulsVisiblesamv 正在遵循原始版本的情况。

我可以避免这种情况吗?也许将其设置为“最终”?可能吗?

谢谢。

最佳答案

每当您看到一个对象的更改反射(reflect)在您认为的副本中时,100% 的问题是您没有副本,而是对原始对象的第二个引用。您需要深入研究 LinearLayout 以确定 .clone() 方法是否已完全实现,或者手动复制所有字段

关于java - 如果我将 LinearLayout 实例化为现有 LinearLayout 的副本,那么一旦我对原始 LinearLayout 执行了一次操作,它也会在副本上完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18317954/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com