wxr
2022-11-24 2af932533ef851bf983385244e9912976dbd4daa
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
package com.mm.android.deviceaddmodule.mobilecommon.dialog;
 
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.TextView;
 
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.mobilecommon.base.BaseDialogFragment;
import com.mm.android.deviceaddmodule.mobilecommon.common.LCConfiguration;
import com.mm.android.deviceaddmodule.mobilecommon.widget.antistatic.spinnerwheel.AbstractWheel;
import com.mm.android.deviceaddmodule.mobilecommon.widget.antistatic.spinnerwheel.OnWheelChangedListener;
import com.mm.android.deviceaddmodule.mobilecommon.widget.antistatic.spinnerwheel.adapters.ArrayWheelAdapter;
 
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
 
/**
 * 单项选择框建议使用SingleWheelPickerDialog(优化滚轮体验)
 */
@Deprecated
public class CommonRollSelectDialog extends BaseDialogFragment implements OnKeyListener {
 
    private TextView mTitle;
    private TextView mConfirmBtn;
    private AbstractWheel mVerticalWheel;
    private TextView mDescription;
    private OnClickListener mConfirmListener;
    private int mSelectedIndex;
    private String[] mWheelData;// wheel 数据源
 
    public static CommonRollSelectDialog newInstance(String dialogTitle, int selectIndex, String description, TreeMap<Integer, String> choiceMap) {
        CommonRollSelectDialog dialog = new CommonRollSelectDialog();
        Bundle bundle = new Bundle();
        bundle.putString(LCConfiguration.COMMON_SELECT_DIALOG_TITLE,dialogTitle);
        bundle.putSerializable(LCConfiguration.COMMON_SOURCE_INFO, choiceMap);
        bundle.putSerializable(LCConfiguration.COMMON_CURRENT_SELECTED_INDEX, selectIndex);
        bundle.putString(LCConfiguration.COMMON_SELECT_DIALOG_DESCRIPTION, description);
        dialog.setArguments(bundle);
        return dialog;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NORMAL, R.style.mobile_common_checks_dialog);
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mobile_common_roll_select_dialog, null);
        initView(view);
        return view;
    }
 
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(true);
 
        Window win = getDialog().getWindow();
        if (win != null) {
            win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            WindowManager.LayoutParams localLayoutParams = win.getAttributes();
            localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            localLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            localLayoutParams.gravity = Gravity.BOTTOM;
            getDialog().getWindow().setAttributes(localLayoutParams);
        }
    }
 
    private void initView(View rootView) {
 
        mTitle = rootView.findViewById(R.id.center_title);
        mVerticalWheel = rootView.findViewById(R.id.wheel_vertical_view);
        mDescription = rootView.findViewById(R.id.description);
        mConfirmBtn = rootView.findViewById(R.id.confirm_btn);
        TextView cancelButton = rootView.findViewById(R.id.cancel_btn);
 
        cancelButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
 
        mConfirmBtn.setOnClickListener(mConfirmListener);
        initData();
    }
 
    private void initData() {
        Bundle bundle = getArguments();
        if (bundle == null) return;
 
        TreeMap<Integer, String> map = (TreeMap<Integer, String>) bundle.getSerializable(LCConfiguration.COMMON_SOURCE_INFO);
        mSelectedIndex = bundle.getInt(LCConfiguration.COMMON_CURRENT_SELECTED_INDEX, -1);
        String description = bundle.getString(LCConfiguration.COMMON_SELECT_DIALOG_DESCRIPTION, "");
        if (map == null || map.size() == 0 || mSelectedIndex == -1) return;
 
        String title = bundle.getString(LCConfiguration.COMMON_SELECT_DIALOG_TITLE, "");
 
        List<Integer> keyList = new ArrayList<>(map.keySet());
        mWheelData = new String[keyList.size()];
        for (int i = 0; i < keyList.size(); i++) {
            Integer key = keyList.get(i);
            if (map.get(key) != null) {
                mWheelData[i] = map.get(key);
            }
        }
 
        mTitle.setText(title);
        mDescription.setText(description);
 
        initWheels();
    }
 
    private void initWheels() {
 
        mVerticalWheel.setViewAdapter(new ArrayWheelAdapter(getActivity(), mWheelData, R.layout.wheel_text, R.id.text));
        mVerticalWheel.setCyclic(false);
        mVerticalWheel.setInterpolator(new AnticipateOvershootInterpolator());
        mVerticalWheel.addChangingListener(new OnWheelChangedListener() {
 
            @Override
            public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
                mSelectedIndex = newValue;
            }
        });
        mVerticalWheel.setCurrentItem(mSelectedIndex);
 
    }
 
    public void setConfirmButtonClickListener(OnClickListener listener) {
        mConfirmListener = listener;
    }
 
    public int getCurrentSelectedIndex() {
        return mSelectedIndex;
    }
 
    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            dismissAllowingStateLoss();
            return false;
        }
        return false;
    }
}