Selaa lähdekoodia

Create TopBar react view only once

fixes #1675
Guy Carmeli 7 vuotta sitten
vanhempi
commit
24fb8db816

+ 24
- 5
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java Näytä tiedosto

@@ -6,6 +6,7 @@ import android.os.Build;
6 6
 import android.support.annotation.NonNull;
7 7
 import android.support.annotation.Nullable;
8 8
 import android.support.design.widget.AppBarLayout;
9
+import android.support.v4.util.Pair;
9 10
 import android.support.v7.app.ActionBar;
10 11
 import android.view.Gravity;
11 12
 import android.view.ViewGroup;
@@ -34,7 +35,7 @@ public class TopBar extends AppBarLayout {
34 35
     protected TopTabs topTabs;
35 36
     private VisibilityAnimator visibilityAnimator;
36 37
     @Nullable
37
-    private ContentView reactView;
38
+    private Pair<String, ContentView> reactView;
38 39
 
39 40
     public TopBar(Context context) {
40 41
         super(context);
@@ -91,15 +92,32 @@ public class TopBar extends AppBarLayout {
91 92
 
92 93
     public void setReactView(@NonNull StyleParams styleParams) {
93 94
         if (styleParams.hasTopBarCustomComponent()) {
94
-            reactView = createReactView(styleParams);
95
+            if (isReactViewAlreadySetAndUnchanged(styleParams)) {
96
+                return;
97
+            }
98
+            unmountReactView();
99
+            reactView = new Pair<>(styleParams.topBarReactView, createReactView(styleParams));
95 100
             if ("fill".equals(styleParams.topBarReactViewAlignment)) {
96
-                addReactViewFill(reactView);
101
+                addReactViewFill(reactView.second);
97 102
             } else {
98
-                addCenteredReactView(reactView);
103
+                addCenteredReactView(reactView.second);
99 104
             }
105
+        } else {
106
+            unmountReactView();
100 107
         }
101 108
     }
102 109
 
110
+    private void unmountReactView() {
111
+        if (reactView == null) return;
112
+        titleBar.removeView(reactView.second);
113
+        reactView.second.unmountReactView();
114
+        reactView = null;
115
+    }
116
+
117
+    private boolean isReactViewAlreadySetAndUnchanged(@NonNull StyleParams styleParams) {
118
+        return reactView != null && styleParams.topBarReactView.equals(reactView.first);
119
+    }
120
+
103 121
     private ContentView createReactView(StyleParams styleParams) {
104 122
         return new ContentView(getContext(),
105 123
                 styleParams.topBarReactView,
@@ -217,7 +235,8 @@ public class TopBar extends AppBarLayout {
217 235
 
218 236
     public void destroy() {
219 237
         if (reactView != null) {
220
-            reactView.unmountReactView();
238
+            reactView.second.unmountReactView();
239
+            reactView = null;
221 240
         }
222 241
         titleBar.destroy();
223 242
     }