Files
react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java
T
Leo Nikkilä 1f88a7111d Fix view indices with Android LayoutAnimation (attempt 2) (#19775)
Summary:
/cc janicduplessis mdvacca

This addresses the same issue as #18830 which was reverted since it didn’t handle `removeClippedSubviews` properly.

When subview clipping is on, ReactViewGroup keeps track of its own children directly and accounts for the offset introduced by clipped views when calling ViewGroup methods that modify children.

Instead of accounting for just clipped children (views with no parent), it should account for any children that aren’t in the ViewGroup which also includes children that are being transitioned. If you look at the ViewGroup source code, [it explicitly retains the view parent until the transition finishes](https://github.com/aosp-mirror/platform_frameworks_base/blob/oreo-release/core/java/android/view/ViewGroup.java#L5034-L5036) which caused the `getParent()` checks to pass, even though those views should be ignored. I added a new `isChildInViewGroup` method that handles both clipped and transitioning views to fix this.

I reproduced the [earlier crash](https://github.com/facebook/react-native/pull/18830#issuecomment-382798628) by enabling clipping in [this test app](https://github.com/facebook/react-native/pull/18830#pullrequestreview-111758886) and adding a “clear views” button that resets the state to an empty items array with an animation.

- #18830

[ANDROID] [BUGFIX] [LayoutAnimation] - Removal LayoutAnimations no longer remove adjacent views as well in certain cases.
Pull Request resolved: https://github.com/facebook/react-native/pull/19775

Differential Revision: D9105838

Pulled By: hramos

fbshipit-source-id: 5ccb0957d1f46c36add960c0e4ef2a545cb03cbe
2018-08-02 07:01:42 -07:00

128 lines
3.6 KiB
Java

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.uimanager;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.WeakHashMap;
import android.view.View;
import android.view.ViewGroup;
import javax.annotation.Nullable;
/**
* Class providing children management API for view managers of classes extending ViewGroup.
*/
public abstract class ViewGroupManager <T extends ViewGroup>
extends BaseViewManager<T, LayoutShadowNode> {
private static WeakHashMap<View, Integer> mZIndexHash = new WeakHashMap<>();
@Override
public LayoutShadowNode createShadowNodeInstance() {
return new LayoutShadowNode();
}
@Override
public Class<? extends LayoutShadowNode> getShadowNodeClass() {
return LayoutShadowNode.class;
}
@Override
public void updateExtraData(T root, Object extraData) {
}
public void addView(T parent, View child, int index) {
parent.addView(child, index);
}
/**
* Convenience method for batching a set of addView calls
* Note that this adds the views to the beginning of the ViewGroup
*
* @param parent the parent ViewGroup
* @param views the set of views to add
*/
public void addViews(T parent, List<View> views) {
for (int i = 0, size = views.size(); i < size; i++) {
addView(parent, views.get(i), i);
}
}
public static void setViewZIndex(View view, int zIndex) {
mZIndexHash.put(view, zIndex);
}
public static @Nullable Integer getViewZIndex(View view) {
return mZIndexHash.get(view);
}
public int getChildCount(T parent) {
return parent.getChildCount();
}
public View getChildAt(T parent, int index) {
return parent.getChildAt(index);
}
public void removeViewAt(T parent, int index) {
parent.removeViewAt(index);
}
public void removeView(T parent, View view) {
for (int i = 0; i < getChildCount(parent); i++) {
if (getChildAt(parent, i) == view) {
removeViewAt(parent, i);
break;
}
}
}
public void removeAllViews(T parent) {
for (int i = getChildCount(parent) - 1; i >= 0; i--) {
removeViewAt(parent, i);
}
}
public void startViewTransition(T parent, View view) {
parent.startViewTransition(view);
}
public void endViewTransition(T parent, View view) {
parent.endViewTransition(view);
}
/**
* Returns whether this View type needs to handle laying out its own children instead of
* deferring to the standard css-layout algorithm.
* Returns true for the layout to *not* be automatically invoked. Instead onLayout will be
* invoked as normal and it is the View instance's responsibility to properly call layout on its
* children.
* Returns false for the default behavior of automatically laying out children without going
* through the ViewGroup's onLayout method. In that case, onLayout for this View type must *not*
* call layout on its children.
*/
public boolean needsCustomLayoutForChildren() {
return false;
}
/**
* Returns whether or not this View type should promote its grandchildren as Views. This is an
* optimization for Scrollable containers when using Nodes, where instead of having one ViewGroup
* containing a large number of draw commands (and thus being more expensive in the case of
* an invalidate or re-draw), we split them up into several draw commands.
*/
public boolean shouldPromoteGrandchildren() {
return false;
}
}