Skip to content

Commit 9b742f3

Browse files
committed
优化代码写法及代码逻辑
1 parent 9bf7009 commit 9b742f3

File tree

15 files changed

+70
-48
lines changed

15 files changed

+70
-48
lines changed

app/src/main/java/com/hjq/demo/ui/activity/SplashActivity.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.animation.Animator;
44
import android.animation.AnimatorListenerAdapter;
5+
import android.annotation.SuppressLint;
56
import android.content.Intent;
67
import android.view.View;
78
import androidx.annotation.NonNull;
@@ -28,6 +29,7 @@
2829
* time : 2018/10/18
2930
* desc : 闪屏界面
3031
*/
32+
@SuppressLint("CustomSplashScreen")
3133
public final class SplashActivity extends AppActivity {
3234

3335
private LottieAnimationView mLottieView;
@@ -106,9 +108,10 @@ protected ImmersionBar createStatusBarConfig() {
106108
.hideBar(BarHide.FLAG_HIDE_BAR);
107109
}
108110

111+
@SuppressLint("MissingSuperCall")
109112
@Override
110113
public void onBackPressed() {
111-
//禁用返回键
114+
// 禁用返回键
112115
//super.onBackPressed();
113116
}
114117

app/src/main/java/com/hjq/demo/ui/adapter/common/NavigationAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public void setOnNavigationListener(@Nullable OnNavigationListener listener) {
7070
* {@link BaseAdapter.OnItemClickListener}
7171
*/
7272

73+
@SuppressLint("NotifyDataSetChanged")
7374
@Override
7475
public void onItemClick(@NonNull RecyclerView recyclerView, @NonNull View itemView, int position) {
7576
if (mSelectedPosition == position) {

app/src/main/java/com/hjq/demo/ui/dialog/common/AddressDialog.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
*/
4141
public final class AddressDialog {
4242

43+
private static final int TYPE_PROVINCE = 0;
44+
private static final int TYPE_CITY = 1;
45+
private static final int TYPE_AREA = 2;
46+
4347
public static final class Builder
4448
extends BaseDialog.Builder<Builder>
4549
implements TabAdapter.OnTabListener,
@@ -149,7 +153,7 @@ public Builder setProvince(String province) {
149153
if (!province.equals(data.get(i).getName())) {
150154
continue;
151155
}
152-
selectedAddress(0, i, false);
156+
selectedAddress(TYPE_PROVINCE, i, false);
153157
break;
154158
}
155159
return this;
@@ -178,7 +182,7 @@ public Builder setCity(String city) {
178182
}
179183
// 避开直辖市,因为选择省的时候已经自动跳过市区了
180184
if (mAdapter.getItem(1).size() > 1) {
181-
selectedAddress(1, i, false);
185+
selectedAddress(TYPE_CITY, i, false);
182186
}
183187
break;
184188
}
@@ -220,22 +224,22 @@ public void onSelected(int recyclerViewPosition, int clickItemPosition) {
220224
@SuppressWarnings("all")
221225
private void selectedAddress(int type, int position, boolean smoothScroll) {
222226
switch (type) {
223-
case 0:
227+
case TYPE_PROVINCE:
224228
// 记录当前选择的省份
225229
mProvince = mAdapter.getItem(type).get(position).getName();
226230
mTabAdapter.setItem(type, mProvince);
227231

228232
mTabAdapter.addItem(getString(R.string.address_hint));
229-
mTabAdapter.setSelectedPosition(type + 1);
233+
mTabAdapter.setSelectedPosition(TYPE_CITY);
230234
mAdapter.addItem(AddressManager.getCityList(mAdapter.getItem(type).get(position).getNext()));
231-
mViewPager2.setCurrentItem(type + 1, smoothScroll);
235+
mViewPager2.setCurrentItem(TYPE_CITY, smoothScroll);
232236

233237
// 如果当前选择的是直辖市,就直接跳过选择城市,直接选择区域
234-
if (mAdapter.getItem(type + 1).size() == 1) {
235-
selectedAddress(type + 1, 0, false);
238+
if (mAdapter.getItem(TYPE_CITY).size() == 1) {
239+
selectedAddress(TYPE_CITY, 0, false);
236240
}
237241
break;
238-
case 1:
242+
case TYPE_CITY:
239243
// 记录当前选择的城市
240244
mCity = mAdapter.getItem(type).get(position).getName();
241245
mTabAdapter.setItem(type, mCity);
@@ -251,13 +255,13 @@ private void selectedAddress(int type, int position, boolean smoothScroll) {
251255

252256
} else {
253257
mTabAdapter.addItem(getString(R.string.address_hint));
254-
mTabAdapter.setSelectedPosition(type + 1);
258+
mTabAdapter.setSelectedPosition(TYPE_AREA);
255259
mAdapter.addItem(AddressManager.getAreaList(mAdapter.getItem(type).get(position).getNext()));
256-
mViewPager2.setCurrentItem(type + 1, smoothScroll);
260+
mViewPager2.setCurrentItem(TYPE_AREA, smoothScroll);
257261
}
258262

259263
break;
260-
case 2:
264+
case TYPE_AREA:
261265
// 记录当前选择的区域
262266
mArea = mAdapter.getItem(type).get(position).getName();
263267
mTabAdapter.setItem(type, mArea);

app/src/main/java/com/hjq/demo/ui/dialog/common/AlbumDialog.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,26 @@ public Builder setListener(@Nullable OnListener listener) {
6868
@Override
6969
public void onItemClick(@NonNull RecyclerView recyclerView, @NonNull View itemView, int position) {
7070
List<AlbumInfo> data = mAdapter.getData();
71-
72-
for (AlbumInfo info : data) {
73-
if (info.isSelect()) {
74-
info.setSelect(false);
75-
break;
71+
for (int i = 0; i < data.size(); i++) {
72+
AlbumInfo albumInfo = data.get(i);
73+
if (!albumInfo.isSelect()) {
74+
continue;
7675
}
76+
albumInfo.setSelect(false);
77+
mAdapter.notifyItemChanged(i);
78+
break;
7779
}
78-
mAdapter.getItem(position).setSelect(true);
79-
mAdapter.notifyDataSetChanged();
80+
81+
AlbumInfo albumInfo = mAdapter.getItem(position);
82+
albumInfo.setSelect(true);
83+
mAdapter.notifyItemChanged(position);
8084

8185
// 延迟消失
8286
postDelayed(() -> {
83-
8487
if (mListener != null) {
85-
mListener.onSelected(getDialog(), position, mAdapter.getItem(position));
88+
mListener.onSelected(getDialog(), position, albumInfo);
8689
}
8790
dismiss();
88-
8991
}, 300);
9092
}
9193

app/src/main/java/com/hjq/demo/ui/dialog/common/SelectDialog.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ private Map<Integer, Object> getSelectSet() {
226226
/**
227227
* {@link BaseAdapter.OnItemClickListener}
228228
*/
229-
229+
@SuppressLint("NotifyDataSetChanged")
230230
@Override
231231
public void onItemClick(@NonNull RecyclerView recyclerView, @NonNull View itemView, int position) {
232232
if (mSelectSet.containsKey(position)) {

app/src/main/java/com/hjq/demo/widget/PlayerView.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,7 @@ public void run() {
190190
/**
191191
* 隐藏提示
192192
*/
193-
private final Runnable mHideMessageRunnable = () -> {
194-
mMessageLayout.setVisibility(GONE);
195-
};
193+
private final Runnable mHideMessageRunnable = () -> mMessageLayout.setVisibility(GONE);
196194

197195
public PlayerView(@NonNull Context context) {
198196
this(context, null);
@@ -579,12 +577,13 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
579577
if (progress != 0) {
580578
// 记录当前播放进度
581579
mCurrentProgress = progress;
582-
} else {
583-
// 如果 Activity 返回到后台,progress 会等于 0,而 mVideoView.getDuration 会等于 -1
584-
// 所以要避免在这种情况下记录当前的播放进度,以便用户从后台返回到前台的时候恢复正确的播放进度
585-
if (mVideoView.getDuration() > 0) {
586-
mCurrentProgress = progress;
587-
}
580+
return;
581+
}
582+
583+
// 如果 Activity 返回到后台,progress 会等于 0,而 mVideoView.getDuration 会等于 -1
584+
// 所以要避免在这种情况下记录当前的播放进度,以便用户从后台返回到前台的时候恢复正确的播放进度
585+
if (mVideoView.getDuration() > 0) {
586+
mCurrentProgress = 0;
588587
}
589588
}
590589

app/src/main/java/com/hjq/demo/widget/webview/BrowserFullScreenController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,12 @@ public void enterFullScreen(@NonNull Activity activity, @Nullable View customVie
4747
mCustomViewCallback = callback;
4848

4949
int currentActivityOrientation = activity.getRequestedOrientation();
50-
int targetActivityOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
5150
// 如果当前 Activity 不是横屏,就将 Activity 设置成横屏
52-
if (currentActivityOrientation != targetActivityOrientation) {
51+
if (currentActivityOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
5352
try {
5453
// 兼容问题:在 Android 8.0 的手机上可以固定 Activity 的方向,但是这个 Activity 不能是透明的,否则就会抛出异常
5554
// 复现场景:只需要给 Activity 主题设置 <item name="android:windowIsTranslucent">true</item> 属性即可
56-
activity.setRequestedOrientation(targetActivityOrientation);
55+
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
5756
} catch (IllegalStateException e) {
5857
// java.lang.IllegalStateException: Only fullscreen activities can request orientation
5958
e.printStackTrace();

app/src/main/java/com/hjq/demo/widget/webview/BrowserViewClient.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest r
5757
/**
5858
* 网页加载错误时回调,需要注意的是:这个方法会在 onPageFinished 之前调用
5959
*/
60+
@SuppressWarnings("deprecation")
6061
@Override
6162
public final void onReceivedError(@NonNull WebView view, int errorCode, @NonNull String description, @NonNull String failingUrl) {
6263
super.onReceivedError(view, errorCode, description, failingUrl);

app/src/main/res-common/layout/permission_description_popup.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@
2121
<TextView
2222
android:layout_width="wrap_content"
2323
android:layout_height="wrap_content"
24-
android:layout_marginLeft="@dimen/dp_15"
24+
android:layout_marginHorizontal="@dimen/dp_15"
2525
android:layout_marginTop="@dimen/dp_12"
26-
android:layout_marginRight="@dimen/dp_12"
2726
android:text="@string/common_permission_description_title"
2827
android:textColor="#333333"
2928
android:textSize="@dimen/sp_14"
@@ -33,9 +32,8 @@
3332
android:id="@+id/tv_permission_description_message"
3433
android:layout_width="wrap_content"
3534
android:layout_height="wrap_content"
36-
android:layout_marginLeft="@dimen/dp_15"
35+
android:layout_marginHorizontal="@dimen/dp_15"
3736
android:layout_marginTop="@dimen/dp_5"
38-
android:layout_marginRight="@dimen/dp_15"
3937
android:layout_marginBottom="@dimen/dp_12"
4038
android:textColor="#666666"
4139
android:textSize="@dimen/sp_13"

library/base/src/main/java/com/hjq/base/BaseDialog.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,6 @@ public BaseDialog create() {
921921
mAnimStyle = BaseDialog.ANIM_RIGHT;
922922
break;
923923
default:
924-
mAnimStyle = BaseDialog.ANIM_DEFAULT;
925924
break;
926925
}
927926
}

0 commit comments

Comments
 (0)