浏览代码

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

Fixes #4710
Guy Carmeli 4 年前
父节点
当前提交
aa7908c57d
没有帐户链接到提交者的电子邮件

+ 9
- 5
lib/android/app/src/main/java/com/reactnativenavigation/views/BottomTabs.java 查看文件

@@ -34,6 +34,11 @@ public class BottomTabs extends AHBottomNavigation {
34 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 42
     @Override
38 43
     protected void createItems() {
39 44
         if (itemsCreationEnabled) {
@@ -43,11 +48,6 @@ public class BottomTabs extends AHBottomNavigation {
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 51
     public void superCreateItems() {
52 52
         super.createItems();
53 53
     }
@@ -96,4 +96,8 @@ public class BottomTabs extends AHBottomNavigation {
96 96
          LinearLayout tabsContainer = findChildByClass(this, LinearLayout.class);
97 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 查看文件

@@ -192,6 +192,26 @@ public class BottomTabsControllerTest extends BaseTest {
192 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 215
     @Test
196 216
     public void mergeOptions_currentTabIndex() {
197 217
         uut.ensureViewIsCreated();