Skip to content

Commit 5ae5280

Browse files
author
maqiang
committed
fix setScreenHeightP() bug and add dialog dismiss callback
1 parent 30244bc commit 5ae5280

File tree

5 files changed

+140
-39
lines changed

5 files changed

+140
-39
lines changed

app/src/main/java/org/ninetripods/mq/study/popup/CommonDialogActivity.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public void showBaseUseDialog(View view) {
9999
.setWindowBackgroundP(0.2f)//设置背景透明度 0.0f-1.0f 1.0f完全不透明
100100
.setCancelable(true)//设置是否屏蔽物理返回键 true不屏蔽 false屏蔽
101101
.setCancelableOutSide(true)//设置dialog外点击是否可以让dialog消失
102+
.setOnDismissListener(new IDialog.OnDismissListener() {
103+
@Override
104+
public void onDismiss(IDialog dialog) {
105+
//监听dialog dismiss的回调
106+
toast("dismiss回调");
107+
}
108+
})
102109
.setBuildChildListener(new IDialog.OnBuildListener() {
103110
//设置子View
104111
@Override

sydialoglib/src/main/java/com/fastgo/sydialoglib/IDialog.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,14 @@ interface OnBuildListener {
1818
interface OnClickListener {
1919
void onClick(IDialog dialog);
2020
}
21+
22+
interface OnDismissListener {
23+
/**
24+
* This method will be invoked when the dialog is dismissed.
25+
*
26+
* @param dialog the dialog that was dismissed will be passed into the
27+
* method
28+
*/
29+
void onDismiss(IDialog dialog);
30+
}
2131
}

sydialoglib/src/main/java/com/fastgo/sydialoglib/SYBaseDialog.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
package com.fastgo.sydialoglib;
22

3-
import android.app.Activity;
43
import android.app.Dialog;
54
import android.app.DialogFragment;
65
import android.content.DialogInterface;
76
import android.graphics.Color;
8-
import android.graphics.Point;
97
import android.graphics.drawable.ColorDrawable;
108
import android.os.Bundle;
119
import android.support.annotation.Nullable;
12-
import android.view.Display;
1310
import android.view.Gravity;
1411
import android.view.KeyEvent;
1512
import android.view.LayoutInflater;
@@ -123,39 +120,6 @@ protected int getAnimRes() {
123120
return 0;
124121
}
125122

126-
private static Point point = new Point();
127-
128-
129-
/**
130-
* 获取屏幕宽度
131-
*
132-
* @param activity Activity
133-
* @return ScreenWidth
134-
*/
135-
public static int getScreenWidth(Activity activity) {
136-
Display display = activity.getWindowManager().getDefaultDisplay();
137-
if (display != null) {
138-
display.getSize(point);
139-
return point.x;
140-
}
141-
return 0;
142-
}
143-
144-
/**
145-
* 获取屏幕高度
146-
*
147-
* @param activity Activity
148-
* @return ScreenHeight
149-
*/
150-
public static int getScreenHeight(Activity activity) {
151-
Display display = activity.getWindowManager().getDefaultDisplay();
152-
if (display != null) {
153-
display.getSize(point);
154-
return point.y;
155-
}
156-
return 0;
157-
}
158-
159123
@Override
160124
public void onDestroy() {
161125
super.onDestroy();

sydialoglib/src/main/java/com/fastgo/sydialoglib/SYDialog.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.view.WindowManager;
1313

1414
import com.fastgo.driver.dialog.sydialoglib.R;
15+
import com.fastgo.sydialoglib.manager.ScreenUtil;
1516

1617

1718
/**
@@ -24,6 +25,7 @@ public class SYDialog extends SYBaseDialog implements IDialog {
2425
private SYDialogController controller;
2526
private IDialog.OnBuildListener buildListener;
2627
private static final String FTag = "dialogTag";
28+
private IDialog.OnDismissListener dismissListener;
2729

2830
public SYDialog() {
2931
controller = new SYDialogController(this);
@@ -93,9 +95,19 @@ public void dismiss() {
9395
}
9496
}
9597

98+
@Override
99+
public void onDestroy() {
100+
//监听dialog的dismiss
101+
if (dismissListener != null) {
102+
dismissListener.onDismiss(this);
103+
}
104+
super.onDestroy();
105+
}
106+
96107
public static class Builder {
97108
private SYDialogController.SYParams params;
98109
private IDialog.OnBuildListener buildListener;
110+
private IDialog.OnDismissListener dismissListener;
99111

100112
public Builder(Context context) {
101113
if (!(context instanceof Activity)) {
@@ -135,7 +147,7 @@ public Builder setDialogView(View dialogView) {
135147
* @return Builder
136148
*/
137149
public Builder setScreenWidthP(float percentage) {
138-
params.dialogWidth = (int) (getScreenWidth((Activity) params.context) * percentage);
150+
params.dialogWidth = (int) (ScreenUtil.getScreenWidth((Activity) params.context) * percentage);
139151
return this;
140152
}
141153

@@ -146,7 +158,18 @@ public Builder setScreenWidthP(float percentage) {
146158
* @return Builder
147159
*/
148160
public Builder setScreenHeightP(float percentage) {
149-
params.dialogHeight = (int) (getScreenHeight((Activity) params.context) * percentage);
161+
params.dialogHeight = (int) (ScreenUtil.getScreenHeight((Activity) params.context) * percentage);
162+
return this;
163+
}
164+
165+
/**
166+
* 监听dialog的dismiss
167+
*
168+
* @param dismissListener IDialog.OnDismissListener
169+
* @return Builder
170+
*/
171+
public Builder setOnDismissListener(IDialog.OnDismissListener dismissListener) {
172+
this.dismissListener = dismissListener;
150173
return this;
151174
}
152175

@@ -313,6 +336,7 @@ private SYDialog create() {
313336
SYDialog dialog = new SYDialog();
314337
params.apply(dialog.controller);
315338
dialog.buildListener = buildListener;
339+
dialog.dismissListener = dismissListener;
316340
return dialog;
317341
}
318342

@@ -341,7 +365,7 @@ private void setDefaultOption() {
341365
params.gravity = Gravity.CENTER;
342366
params.layoutRes = R.layout.layout_dialog_new;
343367
params.dimAmount = 0.5f;
344-
params.dialogWidth = (int) (getScreenWidth((Activity) params.context) * 0.85f);
368+
params.dialogWidth = (int) (ScreenUtil.getScreenWidth((Activity) params.context) * 0.85f);
345369
params.dialogHeight = WindowManager.LayoutParams.WRAP_CONTENT;
346370
}
347371

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.fastgo.sydialoglib.manager;
2+
3+
import android.app.Activity;
4+
import android.content.res.Resources;
5+
import android.graphics.Point;
6+
import android.os.Build;
7+
import android.view.Display;
8+
import android.view.KeyCharacterMap;
9+
import android.view.KeyEvent;
10+
import android.view.ViewConfiguration;
11+
12+
/**
13+
* Created by mq on 2018/10/26 下午6:05
14+
15+
*/
16+
17+
public class ScreenUtil {
18+
19+
20+
private static Point point = new Point();
21+
22+
23+
/**
24+
* 获取屏幕宽度
25+
*
26+
* @param activity Activity
27+
* @return ScreenWidth
28+
*/
29+
public static int getScreenWidth(Activity activity) {
30+
Display display = activity.getWindowManager().getDefaultDisplay();
31+
if (display != null) {
32+
display.getSize(point);
33+
return point.x;
34+
}
35+
return 0;
36+
}
37+
38+
/**
39+
* 获取屏幕高度
40+
*
41+
* @param activity Activity
42+
* @return ScreenHeight
43+
*/
44+
public static int getScreenHeight(Activity activity) {
45+
Display display = activity.getWindowManager().getDefaultDisplay();
46+
if (display != null) {
47+
display.getSize(point);
48+
return point.y - getStatusBarHeight(activity);
49+
}
50+
return 0;
51+
}
52+
53+
/**
54+
* 计算statusBar高度
55+
*
56+
* @param activity Activity
57+
* @return statusBar高度
58+
*/
59+
public static int getStatusBarHeight(Activity activity) {
60+
Resources resources = activity.getResources();
61+
int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
62+
return resources.getDimensionPixelSize(resourceId);
63+
}
64+
65+
/**
66+
* 计算navigationBar高度
67+
*
68+
* @param activity navigationBar高度
69+
* @return navigationBar高度
70+
*/
71+
public static int getNavigationBarHeight(Activity activity) {
72+
Resources resources = activity.getResources();
73+
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
74+
return resources.getDimensionPixelSize(resourceId);
75+
}
76+
77+
78+
public static boolean isNavigationBarShow(Activity activity) {
79+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
80+
Display display = activity.getWindowManager().getDefaultDisplay();
81+
Point size = new Point();
82+
Point realSize = new Point();
83+
display.getSize(size);
84+
display.getRealSize(realSize);
85+
return realSize.y != size.y;
86+
} else {
87+
boolean menu = ViewConfiguration.get(activity).hasPermanentMenuKey();
88+
boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
89+
if (menu || back) {
90+
return false;
91+
} else {
92+
return true;
93+
}
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)