BaseNewDialog.java
2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.wd.common.dialog;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import com.wd.foundation.wdkit.utils.ScreenUtils;
/**
* @author linksus
* @version 1.0.0
* @desc 所有dialog的基类
* @time 8/7 0007 15:16
*/
public abstract class BaseNewDialog extends LayerDialog implements View.OnClickListener {
private WindowManager.LayoutParams layoutParams;
protected Window window;
protected View layoutView;
public BaseNewDialog(@NonNull Context context) {
super(context);
init(context);
}
public BaseNewDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
init(context);
}
private void init(Context context) {
layoutView = LayoutInflater.from(context).inflate(getLayoutRes(), null, false);
setCanceledOnTouchOutside(canceledOnTouchOutside());
setContentView(layoutView);
window = getWindow();
if (window != null) {
layoutParams = getWindow().getAttributes();
layoutParams.gravity = gravity();
layoutParams.width = (int) (ScreenUtils.getDisplaySize((Activity) context).widthPixels /
(pixelsWidthScale() <= 0 ? 1 : pixelsWidthScale()));
if (pixelsHeightScale() > 0) {
layoutParams.height = (int) (ScreenUtils.getDisplaySize((Activity) context).heightPixels /
pixelsHeightScale());
}else{
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
if (dialogAnimation() != 0) {
window.setWindowAnimations(dialogAnimation());
}
window.setAttributes(layoutParams);
}
attachView(layoutView);
attachListener();
}
protected abstract int gravity();
protected abstract float pixelsHeightScale();
protected abstract float regularHeight();
protected abstract float pixelsWidthScale();
protected abstract boolean canceledOnTouchOutside();
protected abstract void attachView(View view);
protected abstract void attachListener();
protected abstract void attachData();
protected abstract void detachView();
protected abstract @StyleRes
int dialogAnimation();
protected <T extends View> T $findViewById(@IdRes int resId) {
return (T) layoutView.findViewById(resId);
}
protected abstract @LayoutRes
int getLayoutRes();
@Override
public void show() {
super.show();
}
@Override
public void dismiss() {
detachView();
super.dismiss();
}
@Override
public void onClick(View v) {
}
}