Browse Source

Pass ActionMenuView as parent to right buttons instead of Toolbar (#3091)

Motivation:
When searching for the button's concrete TextView by label, if the
title has the same text as the buttons, both views are returned.
Guy Carmeli 6 years ago
parent
commit
bee7e1a91b
No account linked to committer's email address

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/views/ContextualMenu.java View File

@@ -47,7 +47,7 @@ public class ContextualMenu extends TitleBar implements LeftButtonOnClickListene
47 47
 
48 48
     private void addButtonsToContextualMenu(List<ContextualMenuButtonParams> buttons, Menu menu) {
49 49
         for (int i = 0; i < buttons.size(); i++) {
50
-            final TitleBarButton button = new ContextualMenuButton(menu, this, buttons.get(i), this);
50
+            final TitleBarButton button = new ContextualMenuButton(menu, getActionMenuView(), buttons.get(i), this);
51 51
             addButtonInReverseOrder(buttons, i, button);
52 52
         }
53 53
     }

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/views/ContextualMenuButton.java View File

@@ -1,8 +1,8 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3
+import android.support.v7.widget.ActionMenuView;
3 4
 import android.view.Menu;
4 5
 import android.view.MenuItem;
5
-import android.view.ViewGroup;
6 6
 
7 7
 import com.reactnativenavigation.params.ContextualMenuButtonParams;
8 8
 
@@ -14,7 +14,7 @@ class ContextualMenuButton extends TitleBarButton {
14 14
         void onClick(int index);
15 15
     }
16 16
 
17
-    ContextualMenuButton(Menu menu, ViewGroup parent, ContextualMenuButtonParams contextualMenuButtonParams, ContextualButtonClickListener contextualButtonClickListener) {
17
+    ContextualMenuButton(Menu menu, ActionMenuView parent, ContextualMenuButtonParams contextualMenuButtonParams, ContextualButtonClickListener contextualButtonClickListener) {
18 18
         super(menu, parent, contextualMenuButtonParams, null);
19 19
         this.contextualMenuButtonParams = contextualMenuButtonParams;
20 20
         this.contextualButtonClickListener = contextualButtonClickListener;

+ 6
- 2
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

@@ -223,7 +223,7 @@ public class TitleBar extends Toolbar {
223 223
 
224 224
     private void addButtonsToTitleBar(String navigatorEventId, Menu menu) {
225 225
         for (int i = 0; i < rightButtons.size(); i++) {
226
-            final TitleBarButton button = new TitleBarButton(menu, this, rightButtons.get(i), navigatorEventId);
226
+            final TitleBarButton button = new TitleBarButton(menu, getActionMenuView(), rightButtons.get(i), navigatorEventId);
227 227
             addButtonInReverseOrder(rightButtons, i, button);
228 228
         }
229 229
     }
@@ -368,7 +368,7 @@ public class TitleBar extends Toolbar {
368 368
     }
369 369
 
370 370
     private void setButtonTextColor() {
371
-        final ActionMenuView buttonsContainer = ViewUtils.findChildByClass(this, ActionMenuView.class);
371
+        final ActionMenuView buttonsContainer = getActionMenuView();
372 372
         if (buttonsContainer != null) {
373 373
             for (int i = 0; i < buttonsContainer.getChildCount(); i++) {
374 374
                 if (buttonsContainer.getChildAt(i) instanceof TextView) {
@@ -378,6 +378,10 @@ public class TitleBar extends Toolbar {
378 378
         }
379 379
     }
380 380
 
381
+    protected ActionMenuView getActionMenuView() {
382
+        return ViewUtils.findChildByClass(this, ActionMenuView.class);
383
+    }
384
+
381 385
     private void setButtonsIconColor() {
382 386
         final Menu menu = getMenu();
383 387
         for (int i = 0; i < menu.size(); i++) {

+ 8
- 11
android/app/src/main/java/com/reactnativenavigation/views/TitleBarButton.java View File

@@ -8,7 +8,6 @@ import android.text.TextUtils;
8 8
 import android.view.Menu;
9 9
 import android.view.MenuItem;
10 10
 import android.view.View;
11
-import android.view.ViewGroup;
12 11
 import android.widget.TextView;
13 12
 
14 13
 import com.reactnativenavigation.NavigationApplication;
@@ -21,29 +20,28 @@ import java.util.ArrayList;
21 20
 class TitleBarButton implements MenuItem.OnMenuItemClickListener {
22 21
 
23 22
     protected final Menu menu;
24
-    protected final ViewGroup parent;
23
+    private final ActionMenuView actionMenuView;
25 24
     private TitleBarButtonParams buttonParams;
26 25
     @Nullable protected String navigatorEventId;
27 26
 
28
-    TitleBarButton(Menu menu, ViewGroup parent, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
27
+    TitleBarButton(Menu menu, ActionMenuView actionMenuView, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
29 28
         this.menu = menu;
30
-        this.parent = parent;
29
+        this.actionMenuView = actionMenuView;
31 30
         this.buttonParams = buttonParams;
32 31
         this.navigatorEventId = navigatorEventId;
33 32
     }
34 33
 
35
-    MenuItem addToMenu(int index) {
34
+    void addToMenu(int index) {
36 35
         MenuItem item = createMenuItem(index);
37 36
         item.setShowAsAction(buttonParams.showAsAction.action);
38 37
         item.setEnabled(buttonParams.enabled);
39 38
         if (buttonParams.hasComponent()) {
40
-            item.setActionView(new TitleBarButtonComponent(parent.getContext(), buttonParams.componentName, buttonParams.componentProps));
39
+            item.setActionView(new TitleBarButtonComponent(actionMenuView.getContext(), buttonParams.componentName, buttonParams.componentProps));
41 40
         }
42 41
         setIcon(item, index);
43 42
         setColor();
44 43
         setFont();
45 44
         item.setOnMenuItemClickListener(this);
46
-        return item;
47 45
     }
48 46
 
49 47
     private MenuItem createMenuItem(int index) {
@@ -66,10 +64,9 @@ class TitleBarButton implements MenuItem.OnMenuItemClickListener {
66 64
     }
67 65
 
68 66
     private void dontShowLabelOnLongPress(final int index) {
69
-        ViewUtils.runOnPreDraw(parent, new Runnable() {
67
+        ViewUtils.runOnPreDraw(actionMenuView, new Runnable() {
70 68
             @Override
71 69
             public void run() {
72
-                ActionMenuView actionMenuView = ViewUtils.findChildByClass(parent, ActionMenuView.class);
73 70
                 if (actionMenuView != null && actionMenuView.getChildAt(index) != null) {
74 71
                     actionMenuView.getChildAt(index).setOnLongClickListener(null);
75 72
                 }
@@ -94,7 +91,7 @@ class TitleBarButton implements MenuItem.OnMenuItemClickListener {
94 91
     }
95 92
 
96 93
     private void setTextColor() {
97
-        ViewUtils.runOnPreDraw(parent, new Runnable() {
94
+        ViewUtils.runOnPreDraw(actionMenuView, new Runnable() {
98 95
             @Override
99 96
             public void run() {
100 97
                 ArrayList<View> outViews = findActualTextViewInMenuByLabel();
@@ -114,7 +111,7 @@ class TitleBarButton implements MenuItem.OnMenuItemClickListener {
114 111
     @NonNull
115 112
     private ArrayList<View> findActualTextViewInMenuByLabel() {
116 113
         ArrayList<View> outViews = new ArrayList<>();
117
-        parent.findViewsWithText(outViews, buttonParams.label, View.FIND_VIEWS_WITH_TEXT);
114
+        actionMenuView.findViewsWithText(outViews, buttonParams.label, View.FIND_VIEWS_WITH_TEXT);
118 115
         return outViews;
119 116
     }
120 117