implement ViewGroup.detachViewFromParent() properly

Temporarily detaching a View from its parent should not call any
callbacks, which was the case with the old implementation. This was
causing infinite recreation loops in some cases.
This commit is contained in:
Julian Winkler
2025-03-23 18:29:30 +01:00
parent 582307f74c
commit 1edb28b0f7

View File

@@ -136,14 +136,27 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
} }
public void detachViewFromParent(int index) { public void detachViewFromParent(int index) {
removeViewInternal(children.get(index)); children.remove(index).parent = null;
} }
public void attachViewToParent(View view, int index, LayoutParams params) { public void attachViewToParent(View view, int index, LayoutParams params) {
addViewInternal(view, index, params); if (!checkLayoutParams(params))
params = generateLayoutParams(params);
view.parent = this;
view.setLayoutParams(params);
if (index < 0)
index = children.size();
children.add(index, view);
} }
protected void removeDetachedView(View child, boolean animate) { protected void removeDetachedView(View child, boolean animate) {
native_removeView(widget, child.widget);
if (isAttachedToWindow())
child.detachFromWindowInternal();
if (onHierarchyChangeListener != null) {
onHierarchyChangeListener.onChildViewRemoved(this, child);
}
} }
protected native void native_addView(long widget, long child, int index, LayoutParams params); protected native void native_addView(long widget, long child, int index, LayoutParams params);
@@ -373,7 +386,8 @@ public class ViewGroup extends View implements ViewParent, ViewManager {
} }
public void detachViewFromParent(View view) { public void detachViewFromParent(View view) {
removeView(view); children.remove(view);
view.parent = null;
} }
public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {} public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) {}