From 6c204dcbea8e3ad694ca9a8c3c256cd5f38802b5 Mon Sep 17 00:00:00 2001 From: Krzysztof Magiera Date: Tue, 3 Nov 2015 08:54:12 -0800 Subject: [PATCH] Update ViewGroupManager interface to use index for remove method. Differential Revision: D2610700 fb-gh-sync-id: b59dfc581d9ca8d29203b5915fb743f3270989ab --- .../uimanager/NativeViewHierarchyManager.java | 14 +-------- .../react/uimanager/ViewGroupManager.java | 4 +-- .../RecyclerViewBackedScrollView.java | 10 ++++--- .../RecyclerViewBackedScrollViewManager.java | 4 +-- .../react/views/view/ReactViewManager.java | 6 ++-- .../react/views/viewpager/ReactViewPager.java | 29 +++++++++++++++++-- .../viewpager/ReactViewPagerManager.java | 15 ++++++++++ 7 files changed, 56 insertions(+), 26 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java index a41b7f726..9114a8cf6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/NativeViewHierarchyManager.java @@ -310,19 +310,7 @@ import com.facebook.react.touch.JSResponderHandler; viewsToAdd, tagsToDelete)); } - View childView = viewManager.getChildAt(viewToManage, indicesToRemove[i]); - if (childView == null) { - throw new IllegalViewOperationException( - "Trying to remove a null view at index:" - + indexToRemove + " view tag: " + tag + "\n detail: " + - constructManageChildrenErrorMessage( - viewToManage, - viewManager, - indicesToRemove, - viewsToAdd, - tagsToDelete)); - } - viewManager.removeView(viewToManage, childView); + viewManager.removeViewAt(viewToManage, indicesToRemove[i]); lastIndexToRemove = indexToRemove; } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java index 669d7b0b4..646f86772 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ViewGroupManager.java @@ -44,8 +44,8 @@ public abstract class ViewGroupManager return parent.getChildAt(index); } - public void removeView(T parent, View child) { - parent.removeView(child); + public void removeViewAt(T parent, int index) { + parent.removeViewAt(index); } /** diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollView.java b/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollView.java index 118cb50d1..4105d61b4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollView.java @@ -169,8 +169,10 @@ public class RecyclerViewBackedScrollView extends RecyclerView { notifyDataSetChanged(); } - public void removeView(View child) { - if (mViews.remove(child)) { + public void removeViewAt(int index) { + View child = mViews.get(index); + if (child != null) { + mViews.remove(index); mTopOffsetsFromLayout.remove(child); child.removeOnLayoutChangeListener(mChildLayoutChangeListener); mTotalChildrenHeight -= child.getMeasuredHeight(); @@ -271,8 +273,8 @@ public class RecyclerViewBackedScrollView extends RecyclerView { ((ReactListAdapter) getAdapter()).addView(child, index); } - /*package*/ void removeViewFromAdapter(View child) { - ((ReactListAdapter) getAdapter()).removeView(child); + /*package*/ void removeViewFromAdapter(int index) { + ((ReactListAdapter) getAdapter()).removeViewAt(index); } /*package*/ View getChildAtFromAdapter(int index) { diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollViewManager.java index 3d49de448..8c0134dad 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/recyclerview/RecyclerViewBackedScrollViewManager.java @@ -48,8 +48,8 @@ public class RecyclerViewBackedScrollViewManager extends } @Override - public void removeView(RecyclerViewBackedScrollView parent, View child) { - parent.removeViewFromAdapter(child); + public void removeViewAt(RecyclerViewBackedScrollView parent, int index) { + parent.removeViewFromAdapter(index); } /** diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java index 33fd63b57..712d7dc3d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/view/ReactViewManager.java @@ -195,16 +195,16 @@ public class ReactViewManager extends ViewGroupManager { } @Override - public void removeView(ReactViewGroup parent, View child) { + public void removeViewAt(ReactViewGroup parent, int index) { boolean removeClippedSubviews = parent.getRemoveClippedSubviews(); if (removeClippedSubviews) { + View child = getChildAt(parent, index); if (child.getParent() != null) { parent.removeView(child); } parent.removeViewWithSubviewClippingEnabled(child); } else { - parent.removeView(child); + parent.removeViewAt(index); } } - } diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java index ad669a7c6..43944c2ba 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPager.java @@ -49,6 +49,19 @@ import com.facebook.react.uimanager.events.NativeGestureUtil; setOffscreenPageLimit(mViews.size()); } + void removeViewAt(int index) { + mViews.remove(index); + notifyDataSetChanged(); + + // TODO(7323049): Remove this workaround once we figure out a way to re-layout some views on + // request + setOffscreenPageLimit(mViews.size()); + } + + View getViewAt(int index) { + return mViews.get(index); + } + @Override public int getCount() { return mViews.size(); @@ -120,11 +133,23 @@ import com.facebook.react.uimanager.events.NativeGestureUtil; return false; } - /* package */ void addViewToAdapter(View child, int index) { + /*package*/ void addViewToAdapter(View child, int index) { getAdapter().addView(child, index); } - /* package */ void setCurrentItemFromJs(int item) { + /*package*/ void removeViewFromAdapter(int index) { + getAdapter().removeViewAt(index); + } + + /*package*/ int getViewCountInAdapter() { + return getAdapter().getCount(); + } + + /*package*/ View getViewFromAdapter(int index) { + return getAdapter().getViewAt(index); + } + + /*package*/ void setCurrentItemFromJs(int item) { mIsCurrentItemFromJs = true; setCurrentItem(item); mIsCurrentItemFromJs = false; diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPagerManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPagerManager.java index 05fd1e76d..0002a7876 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPagerManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/viewpager/ReactViewPagerManager.java @@ -58,4 +58,19 @@ public class ReactViewPagerManager extends ViewGroupManager { public void addView(ReactViewPager parent, View child, int index) { parent.addViewToAdapter(child, index); } + + @Override + public int getChildCount(ReactViewPager parent) { + return parent.getViewCountInAdapter(); + } + + @Override + public View getChildAt(ReactViewPager parent, int index) { + return parent.getViewFromAdapter(index); + } + + @Override + public void removeViewAt(ReactViewPager parent, int index) { + parent.removeViewFromAdapter(index); + } }