Browse Source

Support re-enabling top bar elevation (#2229)

When disabling elevation by setting topBarElevationShadowEnabled to false, this commit allows to re-enable it by setting the same property to true.
Elevation was previously disabled by using setOutlineProvider(null), with ViewOutlineProvider being an Android 5.0+ object responsible for determining how views draw their outline. To allow re-enabling elevation, we keep our original ViewOutlineProvider and restore it when elevation is enabled again.
Roberto Frenna 6 years ago
parent
commit
43d41be456

+ 9
- 6
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java View File

@@ -10,6 +10,7 @@ import android.support.v4.util.Pair;
10 10
 import android.support.v7.app.ActionBar;
11 11
 import android.view.Gravity;
12 12
 import android.view.ViewGroup;
13
+import android.view.ViewOutlineProvider;
13 14
 import android.widget.FrameLayout;
14 15
 
15 16
 import com.facebook.react.bridge.Callback;
@@ -36,12 +37,16 @@ public class TopBar extends AppBarLayout {
36 37
     private VisibilityAnimator visibilityAnimator;
37 38
     @Nullable
38 39
     private Pair<String, ContentView> reactView;
40
+    private ViewOutlineProvider outlineProvider;
39 41
 
40 42
     public TopBar(Context context) {
41 43
         super(context);
42 44
         setId(ViewUtils.generateViewId());
43 45
         createTopBarVisibilityAnimator();
44 46
         createLayout();
47
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
48
+            outlineProvider = getOutlineProvider();
49
+        }
45 50
     }
46 51
 
47 52
     private void createTopBarVisibilityAnimator() {
@@ -167,19 +172,17 @@ public class TopBar extends AppBarLayout {
167 172
         titleBar.setStyle(styleParams);
168 173
         setReactView(styleParams);
169 174
         setTopTabsStyle(styleParams);
170
-        if (!styleParams.topBarElevationShadowEnabled) {
171
-            disableElevationShadow();
172
-        }
175
+        setElevationEnabled(styleParams.topBarElevationShadowEnabled);
173 176
     }
174 177
 
175 178
     private void setTransparent() {
176 179
         setBackgroundColor(Color.TRANSPARENT);
177
-        disableElevationShadow();
180
+        setElevationEnabled(false);
178 181
     }
179 182
 
180
-    private void disableElevationShadow() {
183
+    private void setElevationEnabled (boolean enabled) {
181 184
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
182
-            setOutlineProvider(null);
185
+            setOutlineProvider(enabled ? outlineProvider : null);
183 186
         }
184 187
     }
185 188