lmw
2023-03-11 4df5bb59e5fe9f9d140e5610f7772dd8a05a28d4
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
/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.fuban.driver.utils.citypickerview.picker.widget;
 
import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import androidx.annotation.NonNull;
 
import com.fuban.driver.R;
import com.fuban.driver.utils.citypickerview.picker.widget.configure.PickerOptions;
import com.xuexiang.xui.XUI;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
import uk.co.chrisjenx.calligraphy.HasTypeface;
 
/**
 * 条件选择器
 *
 * @author xuexiang
 * @since 2019/1/1 下午7:09
 */
public class OptionsPickerView<T> extends BasePickerView implements View.OnClickListener, HasTypeface {
 
    private WheelOptions<T> wheelOptions;
 
    private static final String TAG_SUBMIT = "submit";
    private static final String TAG_CANCEL = "cancel";
    int layoutMode = 0; //页面不同
 
 
    public OptionsPickerView(PickerOptions pickerOptions, int layoutMode) {
        super(pickerOptions.context);
        mPickerOptions = pickerOptions;
        this.layoutMode = layoutMode;
        initView(pickerOptions.context);
    }
 
    private void initView(Context context) {
        setDialogOutSideCancelable();
        initViews();
        initAnim();
        initEvents();
        if (mPickerOptions.customListener == null) {
            if (isDialog()) {
                LayoutInflater.from(context).inflate(R.layout.xui_layout_options_dialog, contentContainer);
            } else {
//                if (layoutMode == 0){
                    LayoutInflater.from(context).inflate(R.layout.xui_layout_options, contentContainer);
//                }
 
            }
 
            //顶部标题
            TextView tvTitle = (TextView) findViewById(com.xuexiang.xui.R.id.tvTitle);
            LinearLayout llContent = (LinearLayout) findViewById(com.xuexiang.xui.R.id.ll_content);
            tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
 
                }
            });
 
            //确定和取消按钮
            Button btnSubmit = (Button) findViewById(com.xuexiang.xui.R.id.btnSubmit);
            Button btnCancel = (Button) findViewById(com.xuexiang.xui.R.id.btnCancel);
 
            btnSubmit.setTag(TAG_SUBMIT);
            btnCancel.setTag(TAG_CANCEL);
            btnSubmit.setOnClickListener(this);
            btnCancel.setOnClickListener(this);
 
            //设置文字
            btnSubmit.setText(TextUtils.isEmpty(mPickerOptions.textContentConfirm) ? context.getResources().getString(com.xuexiang.xui.R.string.xui_picker_view_submit) : mPickerOptions.textContentConfirm);
            btnCancel.setText(TextUtils.isEmpty(mPickerOptions.textContentCancel) ? context.getResources().getString(com.xuexiang.xui.R.string.xui_picker_view_cancel) : mPickerOptions.textContentCancel);
            tvTitle.setText(TextUtils.isEmpty(mPickerOptions.textContentTitle) ? "" : mPickerOptions.textContentTitle);//默认为空
 
            //设置color
            btnSubmit.setTextColor(mPickerOptions.textColorConfirm);
            btnCancel.setTextColor(mPickerOptions.textColorCancel);
            tvTitle.setTextColor(mPickerOptions.textColorTitle);
            if (isDialog()) {
                if (TextUtils.isEmpty(tvTitle.getText().toString())) {
                    tvTitle.setVisibility(View.GONE);
                }
            }
            llContent.setBackgroundColor(mPickerOptions.bgColorTitle);
 
            //设置文字大小
            btnSubmit.setTextSize(mPickerOptions.textSizeSubmitCancel);
            btnCancel.setTextSize(mPickerOptions.textSizeSubmitCancel);
            tvTitle.setTextSize(mPickerOptions.textSizeTitle);
        } else {
            mPickerOptions.customListener.customLayout(LayoutInflater.from(context).inflate(mPickerOptions.layoutRes, contentContainer));
        }
 
        // ----滚轮布局
        final LinearLayout optionsPicker = (LinearLayout) findViewById(com.xuexiang.xui.R.id.options_picker);
        optionsPicker.setBackgroundColor(mPickerOptions.bgColorWheel);
 
        wheelOptions = new WheelOptions<>(optionsPicker, mPickerOptions.isRestoreItem);
        if (mPickerOptions.optionsSelectChangeListener != null) {
            wheelOptions.setOptionsSelectChangeListener(mPickerOptions.optionsSelectChangeListener);
        }
 
        wheelOptions.setTextContentSize(mPickerOptions.textSizeContent);
        wheelOptions.setLabels(mPickerOptions.label1, mPickerOptions.label2, mPickerOptions.label3);
        wheelOptions.setTextXOffset(mPickerOptions.x_offset_one, mPickerOptions.x_offset_two, mPickerOptions.x_offset_three);
        wheelOptions.setCyclic(mPickerOptions.cyclic1, mPickerOptions.cyclic2, mPickerOptions.cyclic3);
        if (XUI.getDefaultTypeface() == null) {
            wheelOptions.setTypeface(mPickerOptions.font);
        }
        setOutSideCancelable(mPickerOptions.cancelable);
 
        wheelOptions.setDividerColor(mPickerOptions.dividerColor);
        wheelOptions.setDividerType(mPickerOptions.dividerType);
        wheelOptions.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
        wheelOptions.setTextColorOut(mPickerOptions.textColorOut);
        wheelOptions.setTextColorCenter(mPickerOptions.textColorCenter);
        wheelOptions.isCenterLabel(mPickerOptions.isCenterLabel);
    }
 
    /**
     * 动态设置标题
     *
     * @param text 标题文本内容
     */
    public void setTitleText(String text) {
        TextView tvTitle = (TextView) findViewById(com.xuexiang.xui.R.id.tvTitle);
        if (tvTitle != null) {
            tvTitle.setText(text);
        }
    }
 
    /**
     * 设置默认选中项
     *
     * @param option1
     */
    public void setSelectOptions(int option1) {
        mPickerOptions.option1 = option1;
        reSetCurrentItems();
    }
 
 
    public void setSelectOptions(int option1, int option2) {
        mPickerOptions.option1 = option1;
        mPickerOptions.option2 = option2;
        reSetCurrentItems();
    }
 
    public void setSelectOptions(int option1, int option2, int option3) {
        mPickerOptions.option1 = option1;
        mPickerOptions.option2 = option2;
        mPickerOptions.option3 = option3;
        reSetCurrentItems();
    }
 
    private void reSetCurrentItems() {
        if (wheelOptions != null) {
            wheelOptions.setCurrentItems(mPickerOptions.option1, mPickerOptions.option2, mPickerOptions.option3);
        }
    }
 
    //=======================================联动===========================================//
 
    public void setPicker(List<T> optionsItems) {
        this.setPicker(optionsItems, null, null);
    }
 
    public void setPicker(@NonNull T[] optionsArray) {
        this.setPicker(Arrays.asList(optionsArray), null, null);
    }
 
    public void setPicker(List<T> options1Items, List<List<T>> options2Items) {
        this.setPicker(options1Items, options2Items, null);
    }
 
    public void setPicker(T[] options1Array,
                          T[][] options2Array) {
        List<List<T>> options2Items = new ArrayList<>(options2Array[0].length);
        for (T[] ts : options2Array) {
            options2Items.add(Arrays.asList(ts));
        }
        wheelOptions.setPicker(Arrays.asList(options1Array), options2Items, null);
        reSetCurrentItems();
    }
 
    public void setPicker(List<T> options1Items,
                          List<List<T>> options2Items,
                          List<List<List<T>>> options3Items) {
        wheelOptions.setPicker(options1Items, options2Items, options3Items);
        reSetCurrentItems();
    }
 
    public void setPicker(T[] options1Array,
                          T[][] options2Array,
                          T[][][] options3Array) {
        List<List<T>> options2Items = new ArrayList<>(options2Array[0].length);
        for (T[] ts : options2Array) {
            options2Items.add(Arrays.asList(ts));
        }
        List<List<List<T>>> options3Items = new ArrayList<>(options3Array[0][0].length);
        for (T[][] ts2 : options3Array) {
            List<List<T>> temp = new ArrayList<>(options3Array[0].length);
            for (T[] ts1 : ts2) {
                temp.add(Arrays.asList(ts1));
            }
            options3Items.add(temp);
        }
        wheelOptions.setPicker(Arrays.asList(options1Array), options2Items, options3Items);
        reSetCurrentItems();
    }
 
    //=======================================不联动===========================================//
 
    //不联动情况下调用
    public void setNPicker(@NonNull T[] options1Array,
                           @NonNull T[] options2Array) {
        setNPicker(Arrays.asList(options1Array), Arrays.asList(options2Array));
    }
 
    //不联动情况下调用
    public void setNPicker(List<T> options1Items,
                           List<T> options2Items) {
        wheelOptions.setLinkage(false);
        wheelOptions.setNPicker(options1Items, options2Items, null);
        reSetCurrentItems();
    }
 
    //不联动情况下调用
    public void setNPicker(@NonNull T[] options1Array,
                           @NonNull T[] options2Array,
                           @NonNull T[] options3Array) {
        setNPicker(Arrays.asList(options1Array), Arrays.asList(options2Array), Arrays.asList(options3Array));
    }
 
    //不联动情况下调用
    public void setNPicker(List<T> options1Items,
                           List<T> options2Items,
                           List<T> options3Items) {
        wheelOptions.setLinkage(false);
        wheelOptions.setNPicker(options1Items, options2Items, options3Items);
        reSetCurrentItems();
    }
 
    @Override
    public void onClick(View v) {
        String tag = (String) v.getTag();
        if (tag.equals(TAG_SUBMIT)) {
            if (!returnData()) {
                dismiss();
            }
        } else {
            dismiss();
        }
    }
 
    /**
     * 抽离接口回调的方法
     *
     * @return true:拦截,不消失;false:不拦截,消失
     */
    public boolean returnData() {
        if (mPickerOptions.optionsSelectListener != null) {
            int[] optionsCurrentItems = wheelOptions.getCurrentItems();
            return mPickerOptions.optionsSelectListener.onOptionsSelect(clickView, optionsCurrentItems[0], optionsCurrentItems[1], optionsCurrentItems[2]);
        }
        return false;
    }
 
    @Override
    public boolean isDialog() {
        return mPickerOptions.isDialog;
    }
 
    @Override
    public void setTypeface(Typeface typeface) {
        if (wheelOptions != null) {
            wheelOptions.setTypeface(typeface);
        }
    }
}