Skip to content

Commit c6b099e

Browse files
Resolved comments
1 parent 305a85d commit c6b099e

File tree

2 files changed

+137
-10
lines changed

2 files changed

+137
-10
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,14 @@ import android.view.View
1111
import android.view.ViewGroup
1212
import com.facebook.react.bridge.ReactApplicationContext
1313
import com.facebook.react.bridge.UiThreadUtil
14-
import java.util.*
14+
import java.util.WeakHashMap
15+
1516

1617
public abstract class ViewGroupManager<T : ViewGroup>
1718
@JvmOverloads
1819
constructor(reactContext: ReactApplicationContext? = null) :
1920
BaseViewManager<T, LayoutShadowNode>(reactContext), IViewGroupManager<T> {
2021

21-
public companion object {
22-
private val zIndexHash: WeakHashMap<View, Int> = WeakHashMap()
23-
24-
@JvmStatic
25-
public fun setViewZIndex(view: View, zIndex: Int): Unit = zIndexHash.set(view, zIndex)
26-
27-
@JvmStatic public fun getViewZIndex(view: View?): Int? = zIndexHash[view]
28-
}
29-
3022
public override fun createShadowNodeInstance(): LayoutShadowNode = LayoutShadowNode()
3123

3224
public override fun getShadowNodeClass(): Class<out LayoutShadowNode> =
@@ -60,13 +52,32 @@ constructor(reactContext: ReactApplicationContext? = null) :
6052

6153
public fun removeView(parent: T, view: View) {
6254
UiThreadUtil.assertOnUiThread()
55+
6356
for (i in 0 until getChildCount(parent)) {
6457
if (getChildAt(parent, i) === view) {
58+
6559
removeViewAt(parent, i)
6660
break
6761
}
6862
}
6963
}
7064

65+
/**
66+
* Returns whether this View type needs to handle laying out its own children instead of deferring
67+
* to the standard css-layout algorithm. Returns true for the layout to *not* be automatically
68+
* invoked. Instead onLayout will be invoked as normal and it is the View instance's
69+
* responsibility to properly call layout on its children. Returns false for the default behavior
70+
* of automatically laying out children without going through the ViewGroup's onLayout method. In
71+
* that case, onLayout for this View type must *not* call layout on its children.
72+
*/
7173
public override fun needsCustomLayoutForChildren(): Boolean = false
74+
75+
public companion object {
76+
private val zIndexHash: WeakHashMap<View, Int> = WeakHashMap()
77+
78+
@JvmStatic
79+
public fun setViewZIndex(view: View, zIndex: Int): Unit = zIndexHash.set(view, zIndex)
80+
81+
@JvmStatic public fun getViewZIndex(view: View?): Int? = zIndexHash[view]
82+
}
7283
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager;
9+
10+
import android.view.View;
11+
import android.view.ViewGroup;
12+
import androidx.annotation.Nullable;
13+
import com.facebook.infer.annotation.Nullsafe;
14+
import com.facebook.react.bridge.ReactApplicationContext;
15+
import com.facebook.react.bridge.UiThreadUtil;
16+
import java.util.List;
17+
import java.util.WeakHashMap;
18+
19+
/** Class providing children management API for view managers of classes extending ViewGroup. */
20+
@Nullsafe(Nullsafe.Mode.LOCAL)
21+
public abstract class ViewGroupManager<T extends ViewGroup>
22+
extends BaseViewManager<T, LayoutShadowNode> implements IViewGroupManager<T> {
23+
24+
private static WeakHashMap<View, Integer> mZIndexHash = new WeakHashMap<>();
25+
26+
public ViewGroupManager() {
27+
super(null);
28+
}
29+
30+
public ViewGroupManager(@Nullable ReactApplicationContext reactContext) {
31+
super(reactContext);
32+
}
33+
34+
@Override
35+
public LayoutShadowNode createShadowNodeInstance() {
36+
return new LayoutShadowNode();
37+
}
38+
39+
@Override
40+
public Class<? extends LayoutShadowNode> getShadowNodeClass() {
41+
return LayoutShadowNode.class;
42+
}
43+
44+
@Override
45+
public void updateExtraData(T root, Object extraData) {}
46+
47+
@Override
48+
public void addView(T parent, View child, int index) {
49+
parent.addView(child, index);
50+
}
51+
52+
/**
53+
* Convenience method for batching a set of addView calls Note that this adds the views to the
54+
* beginning of the ViewGroup
55+
*
56+
* @param parent the parent ViewGroup
57+
* @param views the set of views to add
58+
*/
59+
public void addViews(T parent, List<View> views) {
60+
UiThreadUtil.assertOnUiThread();
61+
62+
for (int i = 0, size = views.size(); i < size; i++) {
63+
addView(parent, views.get(i), i);
64+
}
65+
}
66+
67+
public static void setViewZIndex(View view, int zIndex) {
68+
mZIndexHash.put(view, zIndex);
69+
}
70+
71+
public static @Nullable Integer getViewZIndex(@Nullable View view) {
72+
return mZIndexHash.get(view);
73+
}
74+
75+
@Override
76+
public int getChildCount(T parent) {
77+
return parent.getChildCount();
78+
}
79+
80+
@Override
81+
@Nullable
82+
public View getChildAt(T parent, int index) {
83+
return parent.getChildAt(index);
84+
}
85+
86+
@Override
87+
public void removeViewAt(T parent, int index) {
88+
UiThreadUtil.assertOnUiThread();
89+
90+
parent.removeViewAt(index);
91+
}
92+
93+
public void removeView(T parent, View view) {
94+
UiThreadUtil.assertOnUiThread();
95+
96+
for (int i = 0; i < getChildCount(parent); i++) {
97+
if (getChildAt(parent, i) == view) {
98+
removeViewAt(parent, i);
99+
break;
100+
}
101+
}
102+
}
103+
104+
/**
105+
* Returns whether this View type needs to handle laying out its own children instead of deferring
106+
* to the standard css-layout algorithm. Returns true for the layout to *not* be automatically
107+
* invoked. Instead onLayout will be invoked as normal and it is the View instance's
108+
* responsibility to properly call layout on its children. Returns false for the default behavior
109+
* of automatically laying out children without going through the ViewGroup's onLayout method. In
110+
* that case, onLayout for this View type must *not* call layout on its children.
111+
*/
112+
@Override
113+
public boolean needsCustomLayoutForChildren() {
114+
return false;
115+
}
116+
}

0 commit comments

Comments
 (0)