Browse Source

[Android] Fix title glitch and alignment (#4809)

Fix android title glitch and title center alignment issue on layout orientation change
Hadi Mostafapour 5 years ago
parent
commit
1899601fb9

+ 42
- 24
lib/android/app/src/main/java/com/reactnativenavigation/views/titlebar/TitleBar.java View File

29
 
29
 
30
     private TitleBarButtonController leftButtonController;
30
     private TitleBarButtonController leftButtonController;
31
     private View component;
31
     private View component;
32
-    private Alignment mAlignment;
33
-    private CharSequence mTitle;
32
+    private Alignment titleAlignment;
33
+    private Alignment subtitleAlignment;
34
+    private Boolean isTitleChanged = false;
35
+    private Boolean isSubtitleChanged = false;
34
 
36
 
35
     public TitleBar(Context context) {
37
     public TitleBar(Context context) {
36
         super(context);
38
         super(context);
48
     public void setTitle(CharSequence title) {
50
     public void setTitle(CharSequence title) {
49
         clearComponent();
51
         clearComponent();
50
         super.setTitle(title);
52
         super.setTitle(title);
51
-        if (mTitle != title && mAlignment != null) {
52
-            this.setTitleAlignment(mAlignment);
53
-        }
54
-        mTitle = title;
53
+        isTitleChanged = true;
54
+    }
55
+
56
+    @Override
57
+    public void setSubtitle(CharSequence title) {
58
+        super.setSubtitle(title);
59
+        isSubtitleChanged = true;
55
     }
60
     }
56
 
61
 
57
     public String getTitle() {
62
     public String getTitle() {
84
     }
89
     }
85
 
90
 
86
     public void setTitleAlignment(Alignment alignment) {
91
     public void setTitleAlignment(Alignment alignment) {
87
-        mAlignment = alignment;
88
-        TextView title = findTitleTextView();
89
-        if (title == null || title == mTitle) return;
90
-        alignTextView(alignment, title);
92
+        titleAlignment = alignment;
91
     }
93
     }
92
 
94
 
93
     public void setSubtitleTypeface(Typeface typeface) {
95
     public void setSubtitleTypeface(Typeface typeface) {
101
     }
103
     }
102
 
104
 
103
     public void setSubtitleAlignment(Alignment alignment) {
105
     public void setSubtitleAlignment(Alignment alignment) {
104
-        TextView subtitle = findSubtitleTextView();
105
-        if (subtitle == null) return;
106
-        alignTextView(alignment, subtitle);
106
+        subtitleAlignment = alignment;
107
     }
107
     }
108
 
108
 
109
     private void alignTextView(Alignment alignment, TextView view) {
109
     private void alignTextView(Alignment alignment, TextView view) {
110
         Integer direction = view.getParent().getLayoutDirection();
110
         Integer direction = view.getParent().getLayoutDirection();
111
         Boolean isRTL = direction == View.LAYOUT_DIRECTION_RTL;
111
         Boolean isRTL = direction == View.LAYOUT_DIRECTION_RTL;
112
-        int width = view.getResources().getDisplayMetrics().widthPixels;
113
-        view.post(() -> {
114
-            if (alignment == Alignment.Center) {
115
-                view.measure(0, 0);
116
-                //noinspection IntegerDivisionInFloatingPointContext
117
-                view.setX((width - view.getWidth()) / 2);
118
-            } else if (leftButtonController != null) {
119
-                view.setX(isRTL ? (getWidth() - view.getWidth()) - getContentInsetStartWithNavigation() : getContentInsetStartWithNavigation());
120
-            } else {
121
-                view.setX(isRTL ? (getWidth() - view.getWidth()) - UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN) : UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN));
112
+
113
+        if (alignment == Alignment.Center) {
114
+            //noinspection IntegerDivisionInFloatingPointContext
115
+            view.setX((getWidth() - view.getWidth()) / 2);
116
+        } else if (leftButtonController != null) {
117
+            view.setX(isRTL ? (getWidth() - view.getWidth()) - getContentInsetStartWithNavigation() : getContentInsetStartWithNavigation());
118
+        } else {
119
+            view.setX(isRTL ? (getWidth() - view.getWidth()) - UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN) : UiUtils.dpToPx(getContext(), DEFAULT_LEFT_MARGIN));
120
+        }
121
+    }
122
+
123
+    @Override
124
+    protected void onLayout(boolean changed, int l, int t, int r, int b) {
125
+        super.onLayout(changed, l, t, r, b);
126
+
127
+        if(changed || isTitleChanged) {
128
+            TextView title = findTitleTextView();
129
+            if (title != null) {
130
+                this.alignTextView(titleAlignment, title);
122
             }
131
             }
123
-        });
132
+            isTitleChanged = false;
133
+        }
134
+
135
+        if(changed || isSubtitleChanged) {
136
+            TextView subtitle = findSubtitleTextView();
137
+            if (subtitle != null) {
138
+                this.alignTextView(subtitleAlignment, subtitle);
139
+            }
140
+            isSubtitleChanged = false;
141
+        }
124
     }
142
     }
125
 
143
 
126
     @Nullable
144
     @Nullable