diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/ScrollableViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/ScrollableViewProxy.java index 7df01e930c9..4e384b9627f 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/ScrollableViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/ScrollableViewProxy.java @@ -148,28 +148,60 @@ public TiViewProxy[] getViews() @Kroll.setProperty public void setViews(Object views) { - removeAllViews(); + // Clone current view list. + ArrayList oldViewList = new ArrayList<>(this.views); + // Replace all views with the given view collection. + this.views.clear(); if (views instanceof Object[]) { - for (final Object view : (Object[]) views) { - if (view instanceof TiViewProxy) { - addView((TiViewProxy) view); + for (final Object nextObject : (Object[]) views) { + if (nextObject instanceof TiViewProxy) { + TiViewProxy view = (TiViewProxy) nextObject; + if (!this.views.contains(view)) { + view.setActivity(getActivity()); + view.setParent(this); + this.views.add(view); + } } } } + + // Release all of the views that are no longer attached to this scrollable view. + // Note: If given collection contains views in old collection, then do not release them. + for (TiViewProxy oldView : oldViewList) { + if (!this.views.contains(oldView)) { + oldView.releaseViews(); + oldView.setParent(null); + } + } + + // Notify native scrollable view about the view collection change. + if (this.scrollableView != null) { + this.scrollableView.getAdapter().notifyDataSetChanged(); + } } @Kroll.method public void addView(TiViewProxy view) { - if (!this.views.contains(view)) { - view.setActivity(getActivity()); - view.setParent(this); - this.views.add(view); + // Validate argument. + if (view == null) { + return; + } - if (scrollableView != null) { - scrollableView.getAdapter().notifyDataSetChanged(); - } + // Do not continue if already added. + if (this.views.contains(view)) { + return; + } + + // Add given view to collection. + view.setActivity(getActivity()); + view.setParent(this); + this.views.add(view); + + // Notify native scrollable view about the added child view. + if (this.scrollableView != null) { + this.scrollableView.getAdapter().notifyDataSetChanged(); } }