Browse Source

find title TextView by text

Guy Carmeli 7 years ago
parent
commit
bd7ab7c8a2

+ 15
- 2
android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java View File

@@ -84,10 +84,18 @@ public class ViewUtils {
84 84
         }
85 85
     }
86 86
 
87
+    public interface Matcher<T> {
88
+        boolean match(T child);
89
+    }
90
+
87 91
     /**
88 92
      * Returns the first instance of clazz in root
89 93
      */
90 94
     @Nullable public static <T> T findChildByClass(ViewGroup root, Class clazz) {
95
+        return findChildByClass(root, clazz, null);
96
+    }
97
+
98
+    @Nullable public static <T> T findChildByClass(ViewGroup root, Class clazz, Matcher<T> matcher) {
91 99
         for (int i = 0; i < root.getChildCount(); i++) {
92 100
             View view = root.getChildAt(i);
93 101
             if (clazz.isAssignableFrom(view.getClass())) {
@@ -95,9 +103,14 @@ public class ViewUtils {
95 103
             }
96 104
 
97 105
             if (view instanceof ViewGroup) {
98
-                view = findChildByClass((ViewGroup) view, clazz);
106
+                view = (View) findChildByClass((ViewGroup) view, clazz, matcher);
99 107
                 if (view != null && clazz.isAssignableFrom(view.getClass())) {
100
-                    return (T) view;
108
+                    if (matcher == null) {
109
+                        return (T) view;
110
+                    }
111
+                    if (matcher.match((T) view)) {
112
+                        return (T) view;
113
+                    }
101 114
                 }
102 115
             }
103 116
         }

+ 14
- 5
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

@@ -186,12 +186,21 @@ public class TitleBar extends Toolbar {
186 186
     }
187 187
 
188 188
     private void animateTitle(int alpha) {
189
-        getTitleView().animate()
190
-                .alpha(alpha)
191
-                .setDuration(TITLE_VISIBILITY_ANIMATION_DURATION);
189
+        View titleView = getTitleView();
190
+        if (titleView != null) {
191
+            titleView.animate()
192
+                    .alpha(alpha)
193
+                    .setDuration(TITLE_VISIBILITY_ANIMATION_DURATION);
194
+        }
192 195
     }
193 196
 
194
-    private View getTitleView() {
195
-        return getChildAt(0) instanceof TextView ? getChildAt(0) : getChildAt(1);
197
+    @Nullable
198
+    protected View getTitleView() {
199
+        return ViewUtils.findChildByClass(this, TextView.class, new ViewUtils.Matcher<TextView>() {
200
+            @Override
201
+            public boolean match(TextView child) {
202
+                return child.getText().equals(getTitle());
203
+            }
204
+        });
196 205
     }
197 206
 }

+ 4
- 1
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTitleBar.java View File

@@ -30,7 +30,10 @@ public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener
30 30
             ViewUtils.runOnPreDraw(this, new Runnable() {
31 31
                 @Override
32 32
                 public void run() {
33
-                    hideTitle();
33
+                    View titleView = getTitleView();
34
+                    if (titleView != null) {
35
+                        titleView.setAlpha(0);
36
+                    }
34 37
                 }
35 38
             });
36 39
         }