Skip to content

Commit 28c7535

Browse files
janicduplessiside
authored andcommitted
Fix updating a view z-index on Android
Summary: If the z-index was updated after the initial mount, changes would not be reflected because we did not recalculate the z-index mapped child views and redraw the view. This adds code to do that and call it whenever we update z-index. **Test plan** Tested by reproducing the bug with 2 overlapping views that change z-index every second. Made sure it now works properly and z-index changes are reflected. Closes #15203 Differential Revision: D5564832 Pulled By: achen1 fbshipit-source-id: 5b6c20147211ce0b7e8954d60f8614eafe128fb4
1 parent 373f7ed commit 28c7535

4 files changed

Lines changed: 31 additions & 0 deletions

File tree

ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.graphics.Color;
66
import android.os.Build;
77
import android.view.View;
8+
import android.view.ViewParent;
89

910
import com.facebook.react.R;
1011
import com.facebook.react.bridge.ReadableArray;
@@ -80,6 +81,10 @@ public void setElevation(T view, float elevation) {
8081
public void setZIndex(T view, float zIndex) {
8182
int integerZIndex = Math.round(zIndex);
8283
ViewGroupManager.setViewZIndex(view, integerZIndex);
84+
ViewParent parent = view.getParent();
85+
if (parent != null && parent instanceof ReactZIndexedViewGroup) {
86+
((ReactZIndexedViewGroup) parent).updateDrawingOrder();
87+
}
8388
}
8489

8590
@ReactProp(name = PROP_RENDER_TO_HARDWARE_TEXTURE)

ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactZIndexedViewGroup.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ public interface ReactZIndexedViewGroup {
1212
* @return The child view index considering z-index
1313
*/
1414
int getZIndexMappedChildIndex(int index);
15+
16+
/**
17+
* Redraw the view based on updated child z-index. This should be called after updating one of its child
18+
* z-index.
19+
*/
20+
void updateDrawingOrder();
1521
}

ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupDrawingOrderHelper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,17 @@ public int compare(View view1, View view2) {
100100
}
101101
return mDrawingOrderIndices[index];
102102
}
103+
104+
/**
105+
* Recheck all children for z-index changes.
106+
*/
107+
public void update() {
108+
mNumberOfChildrenWithZIndex = 0;
109+
for (int i = 0; i < mViewGroup.getChildCount(); i++) {
110+
if (ViewGroupManager.getViewZIndex(mViewGroup.getChildAt(i)) != null) {
111+
mNumberOfChildrenWithZIndex++;
112+
}
113+
}
114+
mDrawingOrderIndices = null;
115+
}
103116
}

ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewGroup.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,13 @@ public int getZIndexMappedChildIndex(int index) {
419419
}
420420
}
421421

422+
@Override
423+
public void updateDrawingOrder() {
424+
mDrawingOrderHelper.update();
425+
setChildrenDrawingOrderEnabled(mDrawingOrderHelper.shouldEnableCustomDrawingOrder());
426+
invalidate();
427+
}
428+
422429
@Override
423430
public PointerEvents getPointerEvents() {
424431
return mPointerEvents;

0 commit comments

Comments
 (0)