Skip to content

Commit c3f59ae

Browse files
committed
优化和 TitleBar 相关的代码实现
1 parent f1acf03 commit c3f59ae

3 files changed

Lines changed: 28 additions & 27 deletions

File tree

app/src/main/java/com/hjq/demo/action/TitleBarAction.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ public interface TitleBarAction extends OnTitleBarListener {
2020
* 获取标题栏对象
2121
*/
2222
@Nullable
23-
TitleBar getTitleBar();
23+
TitleBar acquireTitleBar();
2424

2525
/**
2626
* 设置标题栏的标题
2727
*/
2828
default void setTitle(@StringRes int id) {
29-
TitleBar titleBar = getTitleBar();
29+
TitleBar titleBar = acquireTitleBar();
3030
if (titleBar == null) {
3131
return;
3232
}
@@ -37,7 +37,7 @@ default void setTitle(@StringRes int id) {
3737
* 设置标题栏的标题
3838
*/
3939
default void setTitle(CharSequence title) {
40-
TitleBar titleBar = getTitleBar();
40+
TitleBar titleBar = acquireTitleBar();
4141
if (titleBar == null) {
4242
return;
4343
}
@@ -48,23 +48,23 @@ default void setTitle(CharSequence title) {
4848
* 设置标题栏的左标题
4949
*/
5050
default void setLeftTitle(int id) {
51-
TitleBar titleBar = getTitleBar();
51+
TitleBar titleBar = acquireTitleBar();
5252
if (titleBar == null) {
5353
return;
5454
}
5555
titleBar.setLeftTitle(id);
5656
}
5757

5858
default void setLeftTitle(CharSequence text) {
59-
TitleBar titleBar = getTitleBar();
59+
TitleBar titleBar = acquireTitleBar();
6060
if (titleBar == null) {
6161
return;
6262
}
6363
titleBar.setLeftTitle(text);
6464
}
6565

6666
default CharSequence getLeftTitle() {
67-
TitleBar titleBar = getTitleBar();
67+
TitleBar titleBar = acquireTitleBar();
6868
if (titleBar == null) {
6969
return "";
7070
}
@@ -75,23 +75,23 @@ default CharSequence getLeftTitle() {
7575
* 设置标题栏的右标题
7676
*/
7777
default void setRightTitle(int id) {
78-
TitleBar titleBar = getTitleBar();
78+
TitleBar titleBar = acquireTitleBar();
7979
if (titleBar == null) {
8080
return;
8181
}
8282
titleBar.setRightTitle(id);
8383
}
8484

8585
default void setRightTitle(CharSequence text) {
86-
TitleBar titleBar = getTitleBar();
86+
TitleBar titleBar = acquireTitleBar();
8787
if (titleBar == null) {
8888
return;
8989
}
9090
titleBar.setRightTitle(text);
9191
}
9292

9393
default CharSequence getRightTitle() {
94-
TitleBar titleBar = getTitleBar();
94+
TitleBar titleBar = acquireTitleBar();
9595
if (titleBar == null) {
9696
return "";
9797
}
@@ -102,15 +102,15 @@ default CharSequence getRightTitle() {
102102
* 设置标题栏的左图标
103103
*/
104104
default void setLeftIcon(int id) {
105-
TitleBar titleBar = getTitleBar();
105+
TitleBar titleBar = acquireTitleBar();
106106
if (titleBar == null) {
107107
return;
108108
}
109109
titleBar.setLeftIcon(id);
110110
}
111111

112112
default void setLeftIcon(Drawable drawable) {
113-
TitleBar titleBar = getTitleBar();
113+
TitleBar titleBar = acquireTitleBar();
114114
if (titleBar == null) {
115115
return;
116116
}
@@ -119,7 +119,7 @@ default void setLeftIcon(Drawable drawable) {
119119

120120
@Nullable
121121
default Drawable getLeftIcon() {
122-
TitleBar titleBar = getTitleBar();
122+
TitleBar titleBar = acquireTitleBar();
123123
if (titleBar == null) {
124124
return null;
125125
}
@@ -130,15 +130,15 @@ default Drawable getLeftIcon() {
130130
* 设置标题栏的右图标
131131
*/
132132
default void setRightIcon(int id) {
133-
TitleBar titleBar = getTitleBar();
133+
TitleBar titleBar = acquireTitleBar();
134134
if (titleBar == null) {
135135
return;
136136
}
137137
titleBar.setRightIcon(id);
138138
}
139139

140140
default void setRightIcon(Drawable drawable) {
141-
TitleBar titleBar = getTitleBar();
141+
TitleBar titleBar = acquireTitleBar();
142142
if (titleBar == null) {
143143
return;
144144
}
@@ -147,7 +147,7 @@ default void setRightIcon(Drawable drawable) {
147147

148148
@Nullable
149149
default Drawable getRightIcon() {
150-
TitleBar titleBar = getTitleBar();
150+
TitleBar titleBar = acquireTitleBar();
151151
if (titleBar == null) {
152152
return null;
153153
}
@@ -157,7 +157,7 @@ default Drawable getRightIcon() {
157157
/**
158158
* 递归获取 ViewGroup 中的 TitleBar 对象
159159
*/
160-
default TitleBar obtainTitleBar(ViewGroup group) {
160+
default TitleBar findTitleBar(ViewGroup group) {
161161
if (group == null) {
162162
return null;
163163
}
@@ -168,7 +168,7 @@ default TitleBar obtainTitleBar(ViewGroup group) {
168168
}
169169

170170
if (view instanceof ViewGroup) {
171-
TitleBar titleBar = obtainTitleBar((ViewGroup) view);
171+
TitleBar titleBar = findTitleBar((ViewGroup) view);
172172
if (titleBar != null) {
173173
return titleBar;
174174
}

app/src/main/java/com/hjq/demo/app/AppActivity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void hideLoadingDialog() {
100100
protected void initLayout() {
101101
super.initLayout();
102102

103-
TitleBar titleBar = getTitleBar();
103+
TitleBar titleBar = acquireTitleBar();
104104
if (titleBar != null) {
105105
titleBar.setOnTitleBarListener(this);
106106
}
@@ -199,16 +199,17 @@ public void setTitle(@StringRes int id) {
199199
@Override
200200
public void setTitle(CharSequence title) {
201201
super.setTitle(title);
202-
if (getTitleBar() != null) {
203-
getTitleBar().setTitle(title);
202+
TitleBar titleBar = acquireTitleBar();
203+
if (titleBar != null) {
204+
titleBar.setTitle(title);
204205
}
205206
}
206207

207208
@Nullable
208209
@Override
209-
public TitleBar getTitleBar() {
210+
public TitleBar acquireTitleBar() {
210211
if (mTitleBar == null) {
211-
mTitleBar = obtainTitleBar(getContentView());
212+
mTitleBar = findTitleBar(getContentView());
212213
}
213214
return mTitleBar;
214215
}
@@ -219,7 +220,7 @@ public TitleBar getTitleBar() {
219220
@Nullable
220221
@Override
221222
public View getImmersionTopView() {
222-
return getTitleBar();
223+
return acquireTitleBar();
223224
}
224225

225226
@Override

app/src/main/java/com/hjq/demo/app/TitleBarFragment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
3434
super.onViewCreated(view, savedInstanceState);
3535

3636
// 设置标题栏点击监听
37-
TitleBar titleBar = getTitleBar();
37+
TitleBar titleBar = acquireTitleBar();
3838
if (titleBar != null) {
3939
titleBar.setOnTitleBarListener(this);
4040
}
@@ -131,16 +131,16 @@ protected boolean isStatusBarDarkFont() {
131131

132132
@Override
133133
@Nullable
134-
public TitleBar getTitleBar() {
134+
public TitleBar acquireTitleBar() {
135135
if (mTitleBar == null || !isLoading()) {
136-
mTitleBar = obtainTitleBar((ViewGroup) getView());
136+
mTitleBar = findTitleBar((ViewGroup) getView());
137137
}
138138
return mTitleBar;
139139
}
140140

141141
@Nullable
142142
@Override
143143
public View getImmersionTopView() {
144-
return getTitleBar();
144+
return acquireTitleBar();
145145
}
146146
}

0 commit comments

Comments
 (0)