Browse Source

Fix BottomTabs size not adjusted after orientation change (#6166)

Fixes #4710
Guy Carmeli 4 years ago
parent
commit
aa7908c57d
No account linked to committer's email address

+ 9
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java View File

34
         if (shouldCreateItems) createItems();
34
         if (shouldCreateItems) createItems();
35
     }
35
     }
36
 
36
 
37
+    @Override
38
+    public void onSizeChanged(int w, int h, int oldw, int oldh) {
39
+        if (hasItemsAndIsMeasured(w, h, oldw, oldh)) createItems();
40
+    }
41
+
37
     @Override
42
     @Override
38
     protected void createItems() {
43
     protected void createItems() {
39
         if (itemsCreationEnabled) {
44
         if (itemsCreationEnabled) {
43
         }
48
         }
44
     }
49
     }
45
 
50
 
46
-    @Override
47
-    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
48
-        // NOOP - don't recreate views on size change
49
-    }
50
-
51
     public void superCreateItems() {
51
     public void superCreateItems() {
52
         super.createItems();
52
         super.createItems();
53
     }
53
     }
96
          LinearLayout tabsContainer = findChildByClass(this, LinearLayout.class);
96
          LinearLayout tabsContainer = findChildByClass(this, LinearLayout.class);
97
         if (tabsContainer != null) tabsContainer.setLayoutDirection(direction.get());
97
         if (tabsContainer != null) tabsContainer.setLayoutDirection(direction.get());
98
     }
98
     }
99
+
100
+    private boolean hasItemsAndIsMeasured(int w, int h, int oldw, int oldh) {
101
+        return w != 0 && h != 0 && (w != oldw || h != oldh) && getItemsCount() > 0;
102
+    }
99
 }
103
 }

+ 20
- 0
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/bottomtabs/BottomTabsControllerTest.java View File

192
         verify(bottomTabs, times(2)).superCreateItems(); // first time when view is created, second time when options are applied
192
         verify(bottomTabs, times(2)).superCreateItems(); // first time when view is created, second time when options are applied
193
     }
193
     }
194
 
194
 
195
+    @Test
196
+    public void onSizeChanged_recreateItemsIfSizeHasChanged() {
197
+        int numberOfPreviousInvocations = 2;
198
+        bottomTabs.onSizeChanged(0, 0, 0, 0);
199
+        verify(bottomTabs, times(numberOfPreviousInvocations)).superCreateItems();
200
+
201
+        bottomTabs.onSizeChanged(100, 0, 0, 0);
202
+        verify(bottomTabs, times(numberOfPreviousInvocations)).superCreateItems();
203
+
204
+        bottomTabs.onSizeChanged(1080, 147, 0, 0);
205
+        verify(bottomTabs, times(numberOfPreviousInvocations + 1)).superCreateItems();
206
+
207
+        bottomTabs.onSizeChanged(1920, 147, 0, 0);
208
+        verify(bottomTabs, times(numberOfPreviousInvocations + 2)).superCreateItems();
209
+
210
+        when(bottomTabs.getItemsCount()).thenReturn(0);
211
+        bottomTabs.onSizeChanged(1080, 147, 0, 0);
212
+        verify(bottomTabs, times(numberOfPreviousInvocations + 2)).superCreateItems();
213
+    }
214
+
195
     @Test
215
     @Test
196
     public void mergeOptions_currentTabIndex() {
216
     public void mergeOptions_currentTabIndex() {
197
         uut.ensureViewIsCreated();
217
         uut.ensureViewIsCreated();