xuxiuxi
2017-05-25 7e7f99a8c753e7a2a70fd854a79bf75b861bdc66
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
/*
 * Copyright (C) 2016 venshine.cn@gmail.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 cn.com.basic.face.dialog.wheelview.widget;
 
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import cn.com.basic.face.dialog.wheelview.adapter.ArrayWheelAdapter;
import cn.com.basic.face.dialog.wheelview.common.WheelConstants;
import cn.com.basic.face.dialog.wheelview.util.WheelUtils;
import cn.com.basic.face.dialog.wheelview.widget.*;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * 滚轮对话框
 *
 * @author venshine
 */
public class WheelViewDialog<T> implements View.OnClickListener {
 
    private TextView mTitle;
 
    private View mLine1, mLine2;
 
    private cn.com.basic.face.dialog.wheelview.widget.WheelView<T> mWheelView;
 
    private cn.com.basic.face.dialog.wheelview.widget.WheelView.WheelViewStyle mStyle;
 
    private TextView mButton;
 
    private AlertDialog mDialog;
 
    private Context mContext;
 
    private OnDialogItemClickListener mOnDialogItemClickListener;
 
    private int mSelectedPos;
 
    private T mSelectedText;
 
    public WheelViewDialog(Context context) {
        mContext = context;
        init();
    }
 
    private void init() {
        LinearLayout layout = new LinearLayout(mContext);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(WheelUtils.dip2px(mContext, 20), 0, WheelUtils.dip2px(mContext, 20), 0);
 
        mTitle = new TextView(mContext);
        mTitle.setTextColor(WheelConstants.DIALOG_WHEEL_COLOR);
        mTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        mTitle.setGravity(Gravity.CENTER);
        LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                WheelUtils.dip2px(mContext, 50));
        layout.addView(mTitle, titleParams);
 
        mLine1 = new View(mContext);
        mLine1.setBackgroundColor(WheelConstants.DIALOG_WHEEL_COLOR);
        LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                WheelUtils.dip2px(mContext, 2));
        layout.addView(mLine1, lineParams);
 
        mWheelView = new cn.com.basic.face.dialog.wheelview.widget.WheelView(mContext);
        mWheelView.setSkin(cn.com.basic.face.dialog.wheelview.widget.WheelView.Skin.Holo);
        mWheelView.setWheelAdapter(new ArrayWheelAdapter(mContext));
        mStyle = new cn.com.basic.face.dialog.wheelview.widget.WheelView.WheelViewStyle();
        mStyle.textColor = Color.GRAY;
        mStyle.selectedTextZoom = 1.2f;
        mWheelView.setStyle(mStyle);
 
        mWheelView.setOnWheelItemSelectedListener(new cn.com.basic.face.dialog.wheelview.widget.WheelView.OnWheelItemSelectedListener<T>() {
            @Override
            public void onItemSelected(int position, T text) {
                mSelectedPos = position;
                mSelectedText = text;
            }
        });
        ViewGroup.MarginLayoutParams wheelParams = new ViewGroup.MarginLayoutParams(LinearLayout.LayoutParams
                .MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(mWheelView, wheelParams);
 
        mLine2 = new View(mContext);
        mLine2.setBackgroundColor(WheelConstants.DIALOG_WHEEL_COLOR);
        LinearLayout.LayoutParams line2Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                WheelUtils.dip2px(mContext, 1f));
        layout.addView(mLine2, line2Params);
 
        mButton = new TextView(mContext);
        mButton.setTextColor(WheelConstants.DIALOG_WHEEL_COLOR);
        mButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        mButton.setGravity(Gravity.CENTER);
        mButton.setClickable(true);
        mButton.setOnClickListener(this);
        mButton.setText("OK");
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                WheelUtils.dip2px(mContext, 45));
        layout.addView(mButton, buttonParams);
 
        mDialog = new AlertDialog.Builder(mContext).create();
        mDialog.setView(layout);
        mDialog.setCanceledOnTouchOutside(false);
    }
 
    /**
     * 点击事件
     *
     * @param onDialogItemClickListener
     * @return
     */
    public WheelViewDialog setOnDialogItemClickListener(OnDialogItemClickListener onDialogItemClickListener) {
        mOnDialogItemClickListener = onDialogItemClickListener;
        return this;
    }
 
    /**
     * 设置dialog外观颜色
     *
     * @param color
     * @return
     */
    public WheelViewDialog setDialogStyle(int color) {
        mTitle.setTextColor(color);
        mLine1.setBackgroundColor(color);
        mLine2.setBackgroundColor(color);
        mButton.setTextColor(color);
        mStyle.selectedTextColor = color;
        mStyle.holoBorderColor = color;
        return this;
    }
 
    /**
     * 设置标题
     *
     * @param title
     * @return
     */
    public WheelViewDialog setTitle(String title) {
        mTitle.setText(title);
        return this;
    }
 
    /**
     * 设置标题颜色
     *
     * @param color
     * @return
     */
    public WheelViewDialog setTextColor(int color) {
        mTitle.setTextColor(color);
        return this;
    }
 
    /**
     * 设置标题大小
     *
     * @param size
     * @return
     */
    public WheelViewDialog setTextSize(int size) {
        mTitle.setTextSize(size);
        return this;
    }
 
    /**
     * 设置按钮文本
     *
     * @param text
     * @return
     */
    public WheelViewDialog setButtonText(String text) {
        mButton.setText(text);
        return this;
    }
 
    /**
     * 设置按钮文本颜色
     *
     * @param color
     * @return
     */
    public WheelViewDialog setButtonColor(int color) {
        mButton.setTextColor(color);
        return this;
    }
 
    /**
     * 设置按钮文本尺寸
     *
     * @param size
     * @return
     */
    public WheelViewDialog setButtonSize(int size) {
        mButton.setTextSize(size);
        return this;
    }
 
    /**
     * 设置数据项显示个数
     *
     * @param count
     */
    public WheelViewDialog setCount(int count) {
        mWheelView.setWheelSize(count);
        return this;
    }
 
    /**
     * 数据项是否循环显示
     *
     * @param loop
     */
    public WheelViewDialog setLoop(boolean loop) {
        mWheelView.setLoop(loop);
        return this;
    }
 
    /**
     * 设置数据项显示位置
     *
     * @param selection
     */
    public WheelViewDialog setSelection(int selection) {
        mWheelView.setSelection(selection);
        return this;
    }
 
    /**
     * 设置数据项
     *
     * @param arrays
     */
    public WheelViewDialog setItems(T[] arrays) {
        return setItems(Arrays.asList(arrays));
    }
 
    /**
     * 设置数据项
     *
     * @param list
     */
    public WheelViewDialog setItems(List<T> list) {
        mWheelView.setWheelData(list);
        return this;
    }
 
    /**
     * 显示
     */
    public WheelViewDialog show() {
        if (!mDialog.isShowing()) {
            mDialog.show();
        }
        return this;
    }
 
    /**
     * 隐藏
     */
    public WheelViewDialog dismiss() {
        if (mDialog.isShowing()) {
            mDialog.dismiss();
        }
        return this;
    }
 
    @Override
    public void onClick(View v) {
        dismiss();
        if (null != mOnDialogItemClickListener) {
            mOnDialogItemClickListener.onItemClick(mSelectedPos, mSelectedText);
        }
    }
 
    public interface OnDialogItemClickListener<T> {
        void onItemClick(int position, T s);
    }
}