24
24
25
25
/**
26
26
* Created by Sai on 15/11/22.
27
- * 精仿iOSPickerViewController控件
27
+ * Fine imitation of iOSPickerViewController control
28
28
*/
29
29
public class BasePickerView {
30
-
31
30
private Context context ;
32
31
protected ViewGroup contentContainer ;
33
- private ViewGroup rootView ;//附加View 的 根View
34
- private ViewGroup dialogView ;//附加Dialog 的 根View
32
+ private ViewGroup rootView ;
33
+ private ViewGroup dialogView ;
35
34
36
35
protected PickerOptions mPickerOptions ;
37
36
private OnDismissListener onDismissListener ;
@@ -44,57 +43,53 @@ public class BasePickerView {
44
43
protected int animGravity = Gravity .BOTTOM ;
45
44
46
45
private Dialog mDialog ;
47
- protected View clickView ;//是通过哪个View弹出的
46
+ protected View clickView ;//Which View pops up through
48
47
private boolean isAnim = true ;
49
48
50
49
public BasePickerView (Context context ) {
51
50
this .context = context ;
52
51
}
53
52
54
-
55
53
protected void initViews () {
56
-
57
54
FrameLayout .LayoutParams params = new FrameLayout .LayoutParams (
58
55
ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT , Gravity .BOTTOM );
59
56
60
57
LayoutInflater layoutInflater = LayoutInflater .from (context );
61
58
if (isDialog ()) {
62
- //如果是对话框模式
63
59
dialogView = (ViewGroup ) layoutInflater .inflate (R .layout .layout_basepickerview , null , false );
64
- //设置界面的背景为透明
65
60
dialogView .setBackgroundColor (Color .TRANSPARENT );
66
- //这个是真正要加载选择器的父布局
61
+ //This is the parent layout that actually loads the selector
67
62
contentContainer = (ViewGroup ) dialogView .findViewById (R .id .content_container );
68
- //设置对话框 默认左右间距屏幕30
63
+ //Settings dialog box Default left and right spacing screen 30
69
64
params .leftMargin = 30 ;
70
65
params .rightMargin = 30 ;
71
66
contentContainer .setLayoutParams (params );
72
- //创建对话框
67
+
73
68
createDialog ();
74
- //给背景设置点击事件,这样当点击内容以外的地方会关闭界面
69
+ //Set a click event for the background so that the interface will be closed when clicking outside the content
75
70
dialogView .setOnClickListener (new View .OnClickListener () {
76
71
@ Override
77
72
public void onClick (View view ) {
78
73
dismiss ();
79
74
}
80
75
});
81
76
} else {
82
- //如果只是要显示在屏幕的下方
83
- //decorView是activity的根View,包含 contentView 和 titleView
77
+ //If you just want to display it at the bottom of the screen
84
78
if (mPickerOptions .decorView == null ) {
85
79
if (context instanceof Activity ) {
86
80
mPickerOptions .decorView = (ViewGroup ) ((Activity ) context ).getWindow ().getDecorView ();
87
81
} else if (context instanceof ContextThemeWrapper && ((ContextThemeWrapper ) context ).getBaseContext () instanceof Activity ) {
82
+ // When showing from inside an AppCompatDialog, context will be a ContextThemeWrapper
88
83
mPickerOptions .decorView = (ViewGroup ) ((Activity ) ((ContextThemeWrapper ) context ).getBaseContext ()).getWindow ().getDecorView ();
89
84
}
90
85
}
91
- //将控件添加到decorView中
86
+ //Add controls to decorView
92
87
rootView = (ViewGroup ) layoutInflater .inflate (R .layout .layout_basepickerview , mPickerOptions .decorView , false );
93
88
rootView .setLayoutParams (new FrameLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
94
89
if (mPickerOptions .outSideColor != -1 ) {
95
90
rootView .setBackgroundColor (mPickerOptions .outSideColor );
96
91
}
97
- //这个是真正要加载时间选取器的父布局
92
+ //This is the parent layout that actually loads the time picker
98
93
contentContainer = (ViewGroup ) rootView .findViewById (R .id .content_container );
99
94
contentContainer .setLayoutParams (params );
100
95
}
@@ -109,10 +104,9 @@ protected void initAnim() {
109
104
protected void initEvents () {
110
105
}
111
106
112
-
113
107
/**
114
- * @param v (是通过哪个View弹出的 )
115
- * @param isAnim 是否显示动画效果
108
+ * @param v (Which View pops up through )
109
+ * @param isAnim Whether to display animation effects
116
110
*/
117
111
public void show (View v , boolean isAnim ) {
118
112
this .clickView = v ;
@@ -129,9 +123,8 @@ public void show(View v) {
129
123
show ();
130
124
}
131
125
132
-
133
126
/**
134
- * 添加View到根视图
127
+ * Add View to root view
135
128
*/
136
129
public void show () {
137
130
if (isDialog ()) {
@@ -146,11 +139,10 @@ public void show() {
146
139
}
147
140
}
148
141
149
-
150
142
/**
151
- * show的时候调用
143
+ * Called during show
152
144
*
153
- * @param view 这个View
145
+ * @param view the view
154
146
*/
155
147
private void onAttached (View view ) {
156
148
mPickerOptions .decorView .addView (view );
@@ -159,19 +151,17 @@ private void onAttached(View view) {
159
151
}
160
152
}
161
153
162
-
163
154
/**
164
- * 检测该View是不是已经添加到根视图
155
+ * Check whether the View has been added to the root view
165
156
*
166
- * @return 如果视图已经存在该View返回true
157
+ * @return This View returns true if the view already exists
167
158
*/
168
159
public boolean isShowing () {
169
160
if (isDialog ()) {
170
161
return false ;
171
162
} else {
172
163
return rootView .getParent () != null || isShowing ;
173
164
}
174
-
175
165
}
176
166
177
167
public void dismiss () {
@@ -183,11 +173,10 @@ public void dismiss() {
183
173
}
184
174
185
175
if (isAnim ) {
186
- //消失动画
176
+ //Disappear animation
187
177
outAnim .setAnimationListener (new Animation .AnimationListener () {
188
178
@ Override
189
179
public void onAnimationStart (Animation animation ) {
190
-
191
180
}
192
181
193
182
@ Override
@@ -197,7 +186,6 @@ public void onAnimationEnd(Animation animation) {
197
186
198
187
@ Override
199
188
public void onAnimationRepeat (Animation animation ) {
200
-
201
189
}
202
190
});
203
191
contentContainer .startAnimation (outAnim );
@@ -206,16 +194,13 @@ public void onAnimationRepeat(Animation animation) {
206
194
}
207
195
dismissing = true ;
208
196
}
209
-
210
-
211
197
}
212
198
213
199
public void dismissImmediately () {
214
-
215
200
mPickerOptions .decorView .post (new Runnable () {
216
201
@ Override
217
202
public void run () {
218
- //从根视图移除
203
+ //Remove from root view
219
204
mPickerOptions .decorView .removeView (rootView );
220
205
isShowing = false ;
221
206
dismissing = false ;
@@ -224,8 +209,6 @@ public void run() {
224
209
}
225
210
}
226
211
});
227
-
228
-
229
212
}
230
213
231
214
private Animation getInAnimation () {
@@ -244,7 +227,6 @@ public BasePickerView setOnDismissListener(OnDismissListener onDismissListener)
244
227
}
245
228
246
229
public void setKeyBackCancelable (boolean isCancelable ) {
247
-
248
230
ViewGroup View ;
249
231
if (isDialog ()) {
250
232
View = dialogView ;
@@ -273,7 +255,6 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
273
255
};
274
256
275
257
protected BasePickerView setOutSideCancelable (boolean isCancelable ) {
276
-
277
258
if (rootView != null ) {
278
259
View view = rootView .findViewById (R .id .outmost_container );
279
260
@@ -288,15 +269,14 @@ protected BasePickerView setOutSideCancelable(boolean isCancelable) {
288
269
}
289
270
290
271
/**
291
- * 设置对话框模式是否可以点击外部取消
272
+ * Set whether the dialog mode can be canceled by clicking outside
292
273
*/
293
274
public void setDialogOutSideCancelable () {
294
275
if (mDialog != null ) {
295
276
mDialog .setCancelable (mPickerOptions .cancelable );
296
277
}
297
278
}
298
279
299
-
300
280
/**
301
281
* Called when the user touch on black overlay, in order to dismiss the dialog.
302
282
*/
@@ -317,13 +297,13 @@ public View findViewById(int id) {
317
297
public void createDialog () {
318
298
if (dialogView != null ) {
319
299
mDialog = new Dialog (context , R .style .custom_dialog2 );
320
- mDialog .setCancelable (mPickerOptions .cancelable );//不能点外面取消,也不能点back取消
300
+ mDialog .setCancelable (mPickerOptions .cancelable );//You cannot click outside to cancel, nor can you click back to cancel.
321
301
mDialog .setContentView (dialogView );
322
302
323
303
Window dialogWindow = mDialog .getWindow ();
324
304
if (dialogWindow != null ) {
325
305
dialogWindow .setWindowAnimations (R .style .picker_view_scale_anim );
326
- dialogWindow .setGravity (Gravity .CENTER );//可以改成Bottom
306
+ dialogWindow .setGravity (Gravity .CENTER );//Can be changed to Bottom
327
307
}
328
308
329
309
mDialog .setOnDismissListener (new DialogInterface .OnDismissListener () {
@@ -353,14 +333,11 @@ public ViewGroup getDialogContainerLayout() {
353
333
return contentContainer ;
354
334
}
355
335
356
-
357
336
public Dialog getDialog () {
358
337
return mDialog ;
359
338
}
360
339
361
-
362
340
public boolean isDialog () {
363
341
return false ;
364
342
}
365
-
366
343
}
0 commit comments