左晓为主开发手持机充值管理机
zuoxiao
2023-12-19 72c257c004d520851205c0cf0482a367c80cae76
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package com.example.pickerviewlibrary.picker;
 
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
 
import com.example.pickerviewlibrary.R;
import com.example.pickerviewlibrary.picker.adapter.DataAdapter;
import com.example.pickerviewlibrary.picker.entity.AddressBean;
import com.example.pickerviewlibrary.picker.entity.PickerData;
import com.example.pickerviewlibrary.picker.listener.OnPickerClickListener;
import com.example.pickerviewlibrary.picker.util.AddressSqLite_DB_Utile;
 
import java.util.ArrayList;
import java.util.List;
 
public class TeaPickerView extends PopupWindow implements View.OnClickListener {
    private FrameLayout contentLayout;
    private RadioButton mTextFirst, mTextSecond, mTextThird, mTextFourth;
    private RadioGroup groupSelect;
    private View line;
    private ListView pickerList;
    private TextView pickerTitleName;
    private TextView pickerConfirm;
    private View view;
    private int index = 1;
    private List<AddressBean> currData;
    private DataAdapter adapter;
    private Activity context;
    private OnPickerClickListener listener;
    private PickerData pickerData;
 
    private int height;
    private int radius = 0;
    private int backgroundColor = Color.parseColor("#ffffff");
    public static int dataSize = 15;
    public static int dataColor;
    public static int dataHeight;
    private float alpha = 0.5f;
    public static boolean contentLine = false;
    public static Drawable contentLineDrawable;
    public static boolean scrollBal = false;
    public static boolean discolour = true;
    public static int discolourColor;
    public static boolean discolourHook = false;
    public static Drawable customHook;
 
    public TeaPickerView(Activity context) {
        super(context);
        this.context = context;
//        this.pickerData = pickerData;
 
        pickerData = new PickerData();
        AddressBean addressBean = new AddressBean();
        addressBean.setName("请选择地址");
        pickerData.setInitSelectText(addressBean);
        initView();
    }
 
    /**
     * 初始化地址数据
     */
    public void initAddressData() {
        List<AddressBean> first = AddressSqLite_DB_Utile.getInit(context).addressDao().findByPcode("41");
        pickerData.setFirstDatas(first);
    }
 
 
    //显示具体的高度(dp),设置0是自适应
    public TeaPickerView setHeights(int mHeight) {
        if (mHeight > 0) {
            height = getPixelsFromDp(mHeight);
        }
        return this;
    }
 
    //显示的高度占屏幕的百分比,高度没有默认值,需要主动设置
    public TeaPickerView setScreenH(int num) {
        if (num > 0) {
            height = getScreenH(context) / num;
        }
        return this;
    }
 
    //设置整体的背景颜色 默认是#ffffff
    public TeaPickerView setBackground(int color) {
        backgroundColor = color;
        return this;
    }
 
    //设置圆角,默认0
    public TeaPickerView setRadius(int mRadius) {
        this.radius = mRadius;
        return this;
    }
 
    //内容栏的背景颜色 默认是#ffffff
    public TeaPickerView setContentBackground(int color) {
        GradientDrawable drawable = new GradientDrawable();
        //设置圆角大小
        drawable.setCornerRadii(new float[]{radius, radius, radius, radius, 0, 0, 0, 0});
        //设置shape背景色
        drawable.setColor(color);
        contentLayout.setBackground(drawable);
        return this;
    }
 
    //内容栏的高度(dp) 默认是50dp
    public TeaPickerView setContentHeight(int mHeight) {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) groupSelect.getLayoutParams();
        params.height = getPixelsFromDp(mHeight);
        groupSelect.setLayoutParams(params);
        return this;
    }
 
    //内容栏字体的大小和颜色, 默认是16sp,#0aa666,此方法不会变换颜色
    public TeaPickerView setContentText(int size, int color) {
        mTextFirst.setTextSize(size);
        mTextFirst.setTextColor(color);
        mTextSecond.setTextSize(size);
        mTextSecond.setTextColor(color);
        mTextThird.setTextSize(size);
        mTextThird.setTextColor(color);
        mTextFourth.setTextSize(size);
        mTextFourth.setTextColor(color);
        return this;
    }
 
    //自定义内容栏字体颜色变换器 在res目录下创建color文件夹用selector 默认颜色#555 选中颜色#0aa666
    public TeaPickerView setContentText(ColorStateList colorStateList) {
        mTextFirst.setTextColor(colorStateList);
        mTextSecond.setTextColor(colorStateList);
        mTextThird.setTextColor(colorStateList);
        mTextFourth.setTextColor(colorStateList);
        return this;
    }
 
    //内容栏选中是否有下划线 默认不开启
    public TeaPickerView setContentLine(boolean bl) {
        contentLine = bl;
        return this;
    }
 
    //自定义内容栏下划线用layer-list 默认是下边框描边 颜色#0fbc72 高度1dp
    public TeaPickerView setContentLineColor(Drawable drawable) {
        contentLineDrawable = drawable;
        return this;
    }
 
    //分割线的高度和颜色 默认是0.5dp #e5e5e5
    public TeaPickerView setLine(int mHeight, int color) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) line.getLayoutParams();
        params.height = getPixelsFromDp(mHeight);
        line.setLayoutParams(params);
        line.setBackgroundColor(color);
        return this;
    }
 
    //设置list的item的高度(dp) 默认是40dp
    public TeaPickerView setitemHeight(int mHeight) {
        dataHeight = getPixelsFromDp(mHeight);
        return this;
    }
 
    //设置list的字体大小和颜色 默认是15 #555
    public TeaPickerView setListText(int size, int color) {
        dataSize = size;
        dataColor = color;
        return this;
    }
 
    //设置list是否显示滚动条,默认false
    public TeaPickerView setScrollBal(boolean bl) {
        scrollBal = bl;
        return this;
    }
 
    //设置阴影层的透明度 默认是0.5f
    public TeaPickerView setAlpha(float mFloat) {
        alpha = mFloat;
        return this;
    }
 
    //设置选中项是否加色,默认true
    public TeaPickerView setDiscolour(boolean bl) {
        discolour = bl;
        return this;
    }
 
    //设置选中项加色的颜色值,默认#0aa666
    public TeaPickerView setDiscolourColor(int color) {
        discolourColor = color;
        return this;
    }
 
    //设置选中项是否有√图标,默认false
    public TeaPickerView setDiscolourHook(boolean bl) {
        discolourHook = bl;
        return this;
    }
 
    //自定义√图标
    public TeaPickerView setCustomHook(Drawable drawable) {
        customHook = drawable;
        return this;
    }
 
    //参数设置完毕,一定要build一下
    public void build() {
        initPicker();
        initData();
    }
 
    private GradientDrawable getSharp() {
        GradientDrawable drawable = new GradientDrawable();
        //设置圆角大小
        drawable.setCornerRadii(new float[]{radius, radius, radius, radius, 0, 0, 0, 0});
        //设置shape背景色
        drawable.setColor(backgroundColor);
        return drawable;
    }
 
    public void setOnPickerClickListener(OnPickerClickListener listener) {
        this.listener = listener;
    }
 
    private void initPicker() {
        this.setContentView(view);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        if (height > 0) {
            this.setHeight(height);
        }
        this.setFocusable(true);
        this.setAnimationStyle(R.style.AnimBottom);
        this.setBackgroundDrawable(getSharp());
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = context.getWindow().getAttributes();
                lp.alpha = 1f;
                context.getWindow().setAttributes(lp);
            }
        });
    }
 
    private void initView() {
        //默认参数
        dataColor = context.getResources().getColor(R.color.picker_text_color);
        dataHeight = getPixelsFromDp(40);
        discolourColor = context.getResources().getColor(R.color.picker_select_text_color);
        contentLineDrawable = context.getResources().getDrawable(R.drawable.tab_sharp);
        //加载view
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.picker_view, null);
        contentLayout = view.findViewById(R.id.contentLayout);
        pickerTitleName = view.findViewById(R.id.pickerTitleName);
        pickerConfirm = view.findViewById(R.id.pickerConfirm);
        groupSelect = view.findViewById(R.id.groupSelect);
        mTextFirst = view.findViewById(R.id.mTextFirst);
        mTextSecond = view.findViewById(R.id.mTextSecond);
        mTextThird = view.findViewById(R.id.mTextThird);
        mTextFourth = view.findViewById(R.id.mTextFourth);
        line = view.findViewById(R.id.line);
        pickerList = view.findViewById(R.id.pickerList);
        mTextFirst.setOnClickListener(this);
        mTextSecond.setOnClickListener(this);
        mTextThird.setOnClickListener(this);
        pickerConfirm.setOnClickListener(this);
 
 
        if (!TextUtils.isEmpty(pickerData.getPickerTitleName())) {
            pickerTitleName.setText(pickerData.getPickerTitleName());
        }
 
    }
 
    public void show(View view) {
        if (pickerData.getFirstDatas() != null) {
            WindowManager.LayoutParams lp = context.getWindow().getAttributes();
            lp.alpha = alpha;
            context.getWindow().setAttributes(lp);
            showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
            switch (index) {
                case 1:
                    adapter.setList(currData, mTextFirst.getText().toString());
                    underlineState();
                    break;
                case 2:
                    adapter.setList(currData, mTextSecond.getText().toString());
                    underlineState();
                    break;
                case 3:
                    adapter.setList(currData, mTextThird.getText().toString());
                    underlineState();
                    break;
                case 4:
                    adapter.setList(currData, mTextFourth.getText().toString());
                    underlineState();
                    break;
            }
        } else {
            Toast.makeText(context, "正在加载地址数据请稍后", Toast.LENGTH_SHORT).show();
        }
    }
 
    private void initData() {
        currData = pickerData.getCurrDatas(index);
        adapter = new DataAdapter(context, currData);
        pickerList.setVerticalScrollBarEnabled(scrollBal);
        pickerList.setAdapter(adapter);
        if (pickerData.getFirstText() != null) {
            mTextFirst.setText(pickerData.getFirstText().getName());
            if (pickerData.getSecondText() != null) {
                mTextSecond.setText(pickerData.getSecondText().getName());
                if (pickerData.getThirdText() != null) {
                    mTextThird.setText(pickerData.getThirdText().getName());
                    if (pickerData.getFourthText() != null)
                        mTextFourth.setText(pickerData.getFourthText().getName());
                }
            }
            mTextFirst.setChecked(true);
        }
        pickerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AddressBean currBean = currData.get(position);
                pickerData.clearSelectText(index);
//                mTextFirst.setText(pickerData.getFirstText().getName());
//                mTextFirst.setTag(pickerData.getFirstText().getCode());
//                mTextSecond.setText(pickerData.getSecondText().getName());
//                mTextThird.setText(pickerData.getThirdText().getName());
                List<AddressBean> data = new ArrayList<>();
 
                data = AddressSqLite_DB_Utile.getInit(context).addressDao().findByPcode(currBean.getCode());
 
                if (index == 1) {
                    pickerData.setFirstText(currBean);
                    mTextFirst.setText(currBean.getName());
                    mTextFirst.setTag(currBean.getCode());
                    groupSelect.check(mTextFirst.getId());
                    pickerData.setSecondDatas(data);
                    new UpdateData(currBean.getName(), data).invoke();
                } else if (index == 2) {
                    pickerData.setSecondText(currBean);
                    mTextSecond.setText(currBean.getName());
                    mTextSecond.setTag(currBean.getCode());
                    groupSelect.check(mTextSecond.getId());
                    pickerData.setThirdDatas(data);
                    new UpdateData(currBean.getName(), data).invoke();
                } else if (index == 3) {
                    pickerData.setThirdText(currBean);
                    mTextThird.setText(currBean.getName());
                    mTextThird.setTag(currBean.getCode());
                    groupSelect.check(mTextThird.getId());
                    pickerData.setFourthDatas(data);
                    new UpdateData(currBean.getName(), data).invoke();
                } else if (index == 4) {
                    pickerData.setFourthText(currBean);
                    mTextFourth.setText(currBean.getName());
                    mTextFourth.setTag(currBean.getCode());
                    groupSelect.check(mTextFourth.getId());
 
                    if (listener != null) {
                        listener.OnPickerClick(pickerData);
                    }
                }
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.mTextFirst) {
            index = 1;
            currData = pickerData.getCurrDatas(index);
            adapter.setList(currData, mTextFirst.getText().toString());
            underlineState();
        } else if (id == R.id.mTextSecond) {
            index = 2;
            currData = pickerData.getCurrDatas(index);
            adapter.setList(currData, mTextSecond.getText().toString());
            underlineState();
        } else if (id == R.id.mTextThird) {
            index = 3;
            currData = pickerData.getCurrDatas(index);
            adapter.setList(currData, mTextThird.getText().toString());
            underlineState();
        } else if (id == R.id.mTextFourth) {
            index = 4;
            currData = pickerData.getCurrDatas(index);
            adapter.setList(currData, mTextFourth.getText().toString());
            underlineState();
        }
        /*else if (id == R.id.pickerConfirm) {
            if (listener != null) {
                dismiss();
                listener.OnPickerConfirmClick(pickerData);
            }
        }*/
    }
 
    private void underlineState() {
        if (!contentLine) {
            return;
        }
        switch (index) {
            case 1:
                mTextFirst.setBackground(contentLineDrawable);
                mTextSecond.setBackground(null);
                mTextThird.setBackground(null);
                mTextFourth.setBackground(null);
                break;
            case 2:
                mTextFirst.setBackground(null);
                mTextSecond.setBackground(contentLineDrawable);
                mTextThird.setBackground(null);
                mTextFourth.setBackground(null);
                break;
            case 3:
                mTextFirst.setBackground(null);
                mTextSecond.setBackground(null);
                mTextThird.setBackground(contentLineDrawable);
                mTextFourth.setBackground(null);
                break;
            case 4:
                mTextFirst.setBackground(null);
                mTextSecond.setBackground(null);
                mTextThird.setBackground(null);
                mTextFourth.setBackground(context.getResources().getDrawable(R.drawable.tab_sharp));
                break;
        }
    }
 
    private class UpdateData {
        private String text;
        private List<AddressBean> data;
 
        /**
         * @param text 地址码
         */
        UpdateData(String text, List<AddressBean> data) {
            this.text = text;
            this.data = data;
        }
 
        void invoke() {
            if (!data.isEmpty()) {
                if (data != null && data.size() > 0) {
                    currData = data;
                    adapter.setList(currData, text);
                    underlineState();
                    index++;
                } else {
                    if (listener != null) {
                        listener.OnPickerClick(pickerData);
                    }
                }
 
            } else {
                if (listener != null) {
                    listener.OnPickerClick(pickerData);
                }
            }
        }
    }
 
 
    public int getScreenW(Context aty) {
        DisplayMetrics dm = aty.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }
 
 
    public int getScreenH(Context aty) {
        DisplayMetrics dm = aty.getResources().getDisplayMetrics();
        return dm.heightPixels;
    }
 
    private int getPixelsFromDp(int size) {
 
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return (size * metrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
    }
}