├── .gitignore
├── LICENSE
├── README-en.md
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── bigkoo
│ │ └── pickerviewdemo
│ │ └── ApplicationTest.java
│ └── main
│ ├── AndroidManifest.xml
│ ├── assets
│ └── province.json
│ ├── java
│ └── com
│ │ └── bigkoo
│ │ └── pickerviewdemo
│ │ ├── FragmentTestActivity.java
│ │ ├── GetJsonDataUtil.java
│ │ ├── JsonDataActivity.java
│ │ ├── MainActivity.java
│ │ ├── TestCircleWheelViewActivity.java
│ │ ├── TestFragment.java
│ │ └── bean
│ │ ├── CardBean.java
│ │ ├── JsonBean.java
│ │ └── ProvinceBean.java
│ └── res
│ ├── drawable-hdpi
│ └── ic_launcher.png
│ ├── drawable-mdpi
│ └── ic_launcher.png
│ ├── drawable-xhdpi
│ ├── ic_launcher.png
│ └── to_down.png
│ ├── drawable-xxhdpi
│ └── ic_launcher.png
│ ├── layout
│ ├── activity_fragmenttest.xml
│ ├── activity_json_data.xml
│ ├── activity_main.xml
│ ├── activity_test_circle_wheelview.xml
│ ├── fragment_test.xml
│ ├── pickerview_custom_lunar.xml
│ ├── pickerview_custom_options.xml
│ └── pickerview_custom_time.xml
│ ├── menu
│ └── menu_main.xml
│ ├── values-en
│ └── strings.xml
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── pickerview
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── bigkoo
│ │ └── pickerview
│ │ └── ApplicationTest.java
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── bigkoo
│ │ └── pickerview
│ │ ├── adapter
│ │ ├── ArrayWheelAdapter.java
│ │ └── NumericWheelAdapter.java
│ │ ├── builder
│ │ ├── OptionsPickerBuilder.java
│ │ └── TimePickerBuilder.java
│ │ ├── configure
│ │ └── PickerOptions.java
│ │ ├── listener
│ │ ├── CustomListener.java
│ │ ├── ISelectTimeCallback.java
│ │ ├── OnDismissListener.java
│ │ ├── OnOptionsSelectChangeListener.java
│ │ ├── OnOptionsSelectListener.java
│ │ ├── OnTimeSelectChangeListener.java
│ │ └── OnTimeSelectListener.java
│ │ ├── utils
│ │ ├── ChinaDate.java
│ │ ├── LunarCalendar.java
│ │ └── PickerViewAnimateUtil.java
│ │ └── view
│ │ ├── BasePickerView.java
│ │ ├── OptionsPickerView.java
│ │ ├── TimePickerView.java
│ │ ├── WheelOptions.java
│ │ └── WheelTime.java
│ └── res
│ ├── anim
│ ├── pickerview_dialog_scale_in.xml
│ ├── pickerview_dialog_scale_out.xml
│ ├── pickerview_slide_in_bottom.xml
│ └── pickerview_slide_out_bottom.xml
│ ├── drawable
│ └── selector_pickerview_btn.xml
│ ├── layout
│ ├── include_pickerview_topbar.xml
│ ├── layout_basepickerview.xml
│ ├── pickerview_options.xml
│ └── pickerview_time.xml
│ ├── values-en
│ └── strings.xml
│ └── values
│ ├── dimens.xml
│ ├── integers.xml
│ ├── strings.xml
│ └── styles.xml
├── preview
├── CustomLayout.gif
├── JsonData.gif
├── Screen Shot 2017-11-09 at 4.25.02 PM.png
├── customHMS.jpeg
├── lunar.gif
├── pickerdemo.gif
├── pickerdemo1x.gif
├── pickerdemo_zhangshangshenghuo.gif
├── timepicker.gif
└── timepicker_night.gif
├── settings.gradle
└── wheelview
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
├── androidTest
└── java
│ └── test
│ └── wheelview
│ └── ExampleInstrumentedTest.java
└── main
├── AndroidManifest.xml
├── java
└── com
│ └── contrarywind
│ ├── adapter
│ └── WheelAdapter.java
│ ├── interfaces
│ └── IPickerViewData.java
│ ├── listener
│ ├── LoopViewGestureListener.java
│ └── OnItemSelectedListener.java
│ ├── timer
│ ├── InertiaTimerTask.java
│ ├── MessageHandler.java
│ └── SmoothScrollTimerTask.java
│ └── view
│ └── WheelView.java
└── res
└── values
├── attrs.xml
├── colors.xml
├── dimens.xml
└── strings.xml
/.gitignore:
--------------------------------------------------------------------------------
1 | # built application files
2 | *.apk
3 | *.ap_
4 |
5 | # files for the dex VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # generated files
12 | bin/
13 | gen/
14 |
15 | # Local configuration file (sdk path, etc)
16 | local.properties
17 |
18 | # Eclipse project files
19 | .classpath
20 | .project
21 |
22 | # Android Studio
23 | .idea/
24 | .gradle
25 | /*/local.properties
26 | /*/out
27 | build
28 | /*/*/production
29 | *.iml
30 | *.iws
31 | *.ipr
32 | *~
33 | *.swp
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README-en.md:
--------------------------------------------------------------------------------
1 |
2 | # Android-PickerView
3 |
4 |
5 |
6 | [](https://android-arsenal.com/api?level=9)
7 | [](http://www.apache.org/licenses/LICENSE-2.0)
8 |
9 | #### Show some :heart: and :stars: the repo to support the project
10 | [](https://github.com/Bigkoo/Android-PickerView/stargazers) [](https://github.com/Bigkoo/Android-PickerView/network) [](https://github.com/Bigkoo/Android-PickerView/watchers)
11 |
12 | #### [中文文档](https://github.com/Bigkoo/Android-PickerView/blob/master/README.md)
13 |
14 | ## Introduction
15 |
16 | This is a library for android to pick date or options like IOS system WheelView widget.
17 | and support for the linkage, dialog . It's very easy to use , you also can customize layout, which make it very customizable.
18 |
19 |
20 | * there are two options called OptionsPickerView and TimePickerView
21 |
22 | * OptionsPickerView supports three levels of linkage
23 |
24 | * TimePickerView support selection date range
25 |
26 | * support "year, month, day, hour, minute, second", "provincial, city, district" and other options of the unit (label) show or hide and customize label.
27 |
28 | * supports custom text, color, text size, etc.
29 |
30 | * If Item text length is too long, it will be adapted to the length of the Item to avoid the problem of incomplete display
31 |
32 |
33 |
34 | # How to use
35 |
36 |
37 | ## 1.Add the dependency:
38 |
39 | ### Gradle
40 |
41 | ```java
42 | compile 'com.contrarywind:Android-PickerView:4.1.9'
43 | ```
44 |
45 | ### Maven
46 |
47 | ```
48 |
49 | com.contrarywind
50 | Android-PickerView
51 | 4.1.8
52 | pom
53 |
54 | ```
55 |
56 | ## 2.Add the following code in your Activity:
57 |
58 | ```java
59 | //TimePicker
60 | pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
61 | @Override
62 | public void onTimeSelect(Date date,View v) {//Callback
63 | tvTime.setText(getTime(date));
64 | }
65 | })
66 | .build();
67 | pvTime.show();
68 | ```
69 |
70 | ```java
71 | //OptionsPicker
72 | pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
73 | @Override
74 | public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
75 | ////Callback
76 | String tx = options1Items.get(options1).getPickerViewText()
77 | + options2Items.get(options1).get(option2)
78 | + options3Items.get(options1).get(option2).get(options3).getPickerViewText();
79 | tvOptions.setText(tx);
80 | }
81 | }).build();
82 |
83 | //pvOptions.setPicker(options1Items);
84 | pvOptions.setPicker(options1Items, options2Items);
85 | //pvOptions.setPicker(options1Items, options2Items, options3Items);
86 | pvOptions.show();
87 | ```
88 | ## Just so easy ~
89 |
90 |
91 | ### Notes(2017-7-10)
92 | - when we start setting the date, we need to pay special attention.
93 |
94 | - reason: the internal component of the Calendar adds 1 processing, which made the month's number of count reduced one.
95 | - error usage case:
96 |
97 | >StartDate.set (2013,1,1);
98 |
99 | >EndDate.set (2020,12,31);
100 |
101 | - correct use case:
102 |
103 | >StartDate.set (2013,0,1);
104 |
105 | >EndDate.set (2020,11,31);
106 |
107 |
108 |
109 | If the default style does not meet your expectations, You can also customize attributes to apply
110 |
111 | ## Customize Useage:
112 | ```java
113 | Calendar selectedDate = Calendar.getInstance();
114 | Calendar startDate = Calendar.getInstance();
115 | startDate.set(2013,0,1);
116 | Calendar endDate = Calendar.getInstance();
117 | endDate.set(2020,11,1);
118 |
119 | pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
120 | @Override
121 | public void onTimeSelect(Date date,View v) {//callback
122 | tvTime.setText(getTime(date));
123 | }
124 | })
125 | .setType(new boolean[]{false, false, false, true, true, false})// type of date
126 | .setCancelText("Cancel")
127 | .setSubmitText("Sure")
128 | .setContentSize(18)
129 | .setTitleSize(20)
130 | .setTitleText("Title")
131 | .setOutSideCancelable(false)// default is true
132 | .isCyclic(true)// default is false
133 | .setTitleColor(Color.BLACK)
134 | .setSubmitColor(Color.BLUE)
135 | .setCancelColor(Color.BLUE)
136 | .setTitleBgColor(0xFF666666)//night mode
137 | .setBgColor(0xFF333333)//night mode
138 | .setRangDate(startDate,endDate)
139 | .setLabel("year","month","day","hours","mins","seconds")
140 | .build();
141 | ```
142 |
143 | ```java
144 | pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
145 | @Override
146 | public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
147 | ////Callback
148 | String tx = options1Items.get(options1).getPickerViewText()
149 | + options2Items.get(options1).get(option2)
150 | + options3Items.get(options1).get(option2).get(options3).getPickerViewText();
151 | tvOptions.setText(tx);
152 | }
153 | })
154 | .setSubmitText("sure")
155 | .setCancelText("cancel")
156 | .setTitleText("title")
157 | .setSubCalSize(18)
158 | .setTitleSize(20)
159 | .setTitleColor(Color.BLACK)
160 | .setSubmitColor(Color.BLUE)
161 | .setCancelColor(Color.BLUE)
162 | .setTitleBgColor(0xFF666666)//night mode
163 | .setBgColor(0xFF444444)//night mode
164 | .setContentTextSize(18)
165 | .setLinkage(false)
166 | .isCenterLabel(false) //default is true , if you choose false , the label text will add to all item ContentText right
167 | .setLabels("province", "city", "district")
168 | .setCyclic(false, false, false)
169 | .setSelectOptions(0, 0, 0) //default options
170 | .setOutSideCancelable(false)//dismiss, default is true
171 | .isRestoreItem(true)// restore option with first item when select changed。
172 | .build();
173 |
174 | pvOptions.setPicker(options1Items, options2Items, options3Items);
175 | ```
176 | ## Customize Layout:
177 | ```java
178 | private void initCustomTimePicker() {
179 | // be careful:In the custom layout, the layout of the ID for optionspicker
180 | // or TimePicker and its child widget must not be modified,
181 | // otherwise will be reported NullPointerException
182 | // For more details, Please refer to the two custom layouts in demo
183 |
184 | Calendar selectedDate = Calendar.getInstance();//System current time
185 | Calendar startDate = Calendar.getInstance();
186 | startDate.set(2013,1,23);
187 | Calendar endDate = Calendar.getInstance();
188 | endDate.set(2019,2,28);
189 |
190 | pvCustomTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
191 | @Override
192 | public void onTimeSelect(Date date, View v) {//call back
193 | btn_CustomTime.setText(getTime(date));
194 | }
195 | }) .setType(new boolean[]{true, true, true, false, false, false})// year - month - day
196 | .setDate(selectedDate)
197 | .setRangDate(startDate,endDate)
198 | .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {
199 |
200 | @Override
201 | public void customLayout(View v) {
202 | final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
203 | ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
204 | tvSubmit.setOnClickListener(new View.OnClickListener() {
205 | @Override
206 | public void onClick(View v) {
207 | pvCustomTime.returnData(tvSubmit);
208 | }
209 | });
210 | ivCancel.setOnClickListener(new View.OnClickListener() {
211 | @Override
212 | public void onClick(View v) {
213 | pvCustomTime.dismiss();
214 | }
215 | });
216 | }
217 | })
218 | .setDividerColor(Color.BLACK)
219 | .build();
220 |
221 | }
222 | ```
223 |
224 | ## If you need to set the non-linkage data:
225 |
226 | ```java
227 | pvNoLinkOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
228 |
229 | @Override
230 | public void onOptionsSelect(int options1, int options2, int options3, View v) {
231 |
232 | String str = "food:"+food.get(options1)
233 | +"\nclothes:"+clothes.get(options2)
234 | +"\ncomputer:"+computer.get(options3);
235 |
236 | Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();
237 | }
238 | }).build();
239 | pvNoLinkOptions.setNPicker(food,clothes,computer);
240 | pvNoLinkOptions.show();
241 | ```
242 |
243 | ## For more detail, please refer to the Demo code, If there is still doubt about you, please [New Issue](https://github.com/Bigkoo/Android-PickerView/issues)
244 |
245 | ### [Here is demo code](https://github.com/Bigkoo/Android-PickerView/blob/master/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java)
246 |
247 | ### [Methods-and-parameters](https://github.com/Bigkoo/Android-PickerView/wiki/Methods-and-parameters)
248 |
249 |
250 | ## Thanks
251 |
252 | - [WheelView](https://github.com/venshine/WheelView)
253 | - [androidWheelView](https://github.com/weidongjian/androidWheelView/)
254 |
255 | # License
256 |
257 | ```
258 | Copyright 2014 Bigkoo
259 | Licensed under the Apache License, Version 2.0 (the "License");
260 | you may not use this file except in compliance with the License.
261 | You may obtain a copy of the License at
262 |
263 | http://www.apache.org/licenses/LICENSE-2.0
264 |
265 | Unless required by applicable law or agreed to in writing, software
266 | distributed under the License is distributed on an "AS IS" BASIS,
267 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
268 | See the License for the specific language governing permissions and
269 | limitations under the License.
270 | ```
271 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## 基于`v4.1.9`支持自定义时分秒,实现时间范围、时间间隔效果
2 |
3 |
4 | 
5 |
6 | **实现上图效果仅需:**
7 | ```
8 | .setHourList(new ArrayList<>(Arrays.asList(9, 10, 11, 12, 13, 14, 15, 16, 17, 18)))
9 | .setMinuteList(new ArrayList<>(Arrays.asList(0, 10, 20, 30, 40, 50)))
10 | ```
11 |
12 | **总共添加了三个API:**
13 | ```
14 | setHourList(List hourList) // 范围:0-23
15 | setMinuteList(List minuteList) // 范围:0-59
16 | setSecondList(List secondList) // 范围:0-59
17 | ```
18 | ### 把原有地址换成下面的即可,其他内容一模一样:
19 | ```
20 | compile 'com.github.princekin-f:Android-PickerView:0.0.1'
21 | ```
22 | **以下是原文档,向大佬致敬!**
23 |
24 |
25 |
26 | ---
27 |
28 |
29 |
30 |
31 |
32 | ## Android-PickerView
33 |
34 | [](https://android-arsenal.com/api?level=9)
35 | [](https://www.apache.org/licenses/LICENSE-2.0)
36 | [ ](https://bintray.com/contrarywind/maven/Android-PickerView/_latestVersion)
37 |
38 | [](https://github.com/Bigkoo/Android-PickerView/stargazers) [](https://github.com/Bigkoo/Android-PickerView/network) [](https://github.com/Bigkoo/Android-PickerView/watchers)
39 |
40 | ### [English Document](https://github.com/Bigkoo/Android-PickerView/blob/master/README-en.md)
41 |
42 | ### 注意事项、详请使用方式、更新日志等,请查看 [Wiki文档](https://github.com/Bigkoo/Android-PickerView/wiki)
43 | **Wiki文档,Wiki文档,Wiki文档 !~ 重要的事情说三遍**
44 |
45 | #### 对于使用上有任何疑问或优化建议等,欢迎加入QQ群讨论交流技术问题。
46 |
47 | 交流群1: 387051294(推荐)
48 |
49 | 交流群2: 219962328(已满)
50 |
51 | ## 介绍
52 |
53 | 这是一款仿iOS的PickerView控件,有时间选择器和选项选择器,新版本的详细特性如下:
54 |
55 | ——TimePickerView 时间选择器,支持年月日时分,年月日,年月,时分等格式。
56 | ——OptionsPickerView 选项选择器,支持一,二,三级选项选择,并且可以设置是否联动 。
57 |
58 | * 支持三级联动
59 | * 设置是否联动
60 | * 设置循环模式
61 | * 支持自定义布局。
62 | * 支持item的分隔线设置。
63 | * 支持item间距设置。
64 | * 时间选择器支持起始和终止日期设定。
65 | * 支持“年,月,日,时,分,秒”,“省,市,区”等选项的单位(label)显示、隐藏和自定义。
66 | * 支持自定义文字、颜色、文字大小等属性
67 | * Item的文字长度过长时,文字会自适应缩放到Item的长度,避免显示不完全的问题
68 | * 支持Dialog 模式。
69 | * 支持自定义设置容器。
70 | * 实时回调。
71 |
72 |
73 | 
74 | 
75 | 
76 | 
77 | 
78 | 
79 |
80 |
81 | ### 有兴趣研究3D滚轮效果的实现机制,希望把源码研究透彻的可以看看这篇博客:
82 | ### [Android-PickerView系列之源码解析篇(二)](http://blog.csdn.net/qq_22393017/article/details/59488906)
83 |
84 | ### 使用注意事项
85 | * 注意:当我们进行设置时间的启始位置时,需要特别注意月份的设定
86 | * 原因:Calendar组件内部的月份,是从0开始的,即0-11代表1-12月份
87 | * 错误使用案例:
88 | startDate.set(2013,1,1);
89 | endDate.set(2020,12,1);
90 | * 正确使用案例:
91 | startDate.set(2013,0,1);
92 | endDate.set(2020,11,1);
93 |
94 | #### V4.1.9 版本更新说明(2019-10-20)
95 | - 修复: 农历 day 偶现越界的问题。
96 | - 优化: 显示布局中英文默认大写问题。
97 | - 新增: 最大可见项的数目提供API给开发者设置。(setItemVisibleCount())
98 | - 新增: 滚轮从中间到两边透明度渐变,提供开关API设置。(isAlphaGradient(true))
99 | - 新增: 选中项圆形分割线样式。(DividerType.CIRCLE)
100 |
101 | #### V4.1.8 版本更新说明(2019-4-24)
102 | - 更新gradle版本, wheelview基础库由 compile 改为 api 依赖,避免gradle 5.0+版本无法引入。
103 | - 修复 setTextXOffset 赋值问题。
104 |
105 | #### V4.1.7 版本更新说明(2019-1-10)
106 | - 修复 WheelView在初始化时,数据为空导致height=0,造成一直显示不出来的问题。
107 | - 新增取消按钮的点击事件监听入口。
108 | - 参数注解添加,规范数据类型。
109 | - 废弃setBackgroundId方法, 更新方法命名为 setOutSideColor。
110 |
111 |
112 | #### 更多历史版本详情,请查阅:[更新日志(4.x版本)](https://github.com/Bigkoo/Android-PickerView/wiki/更新日志(4.x版本))
113 |
114 | #### 方法名与参数请查阅:[方法名与参数说明文档](https://github.com/Bigkoo/Android-PickerView/wiki/%E6%96%B9%E6%B3%95%E5%90%8D%E4%B8%8E%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E%EF%BC%883.x%E7%89%88%E6%9C%AC%EF%BC%89)
115 |
116 |
117 |
118 | ### **如何使用:**
119 |
120 | #### Android-PickerView 库使用示例:
121 |
122 | #### 1.添加Jcenter仓库 Gradle依赖:
123 | ```java
124 | compile 'com.contrarywind:Android-PickerView:4.1.9'
125 | ```
126 | 或者
127 |
128 | #### Maven
129 | ```
130 |
131 | com.contrarywind
132 | Android-PickerView
133 | 4.1.9
134 | pom
135 |
136 | ```
137 |
138 | #### 2.在项目中添加如下代码:
139 |
140 | ```java
141 | //时间选择器
142 | TimePickerView pvTime = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
143 | @Override
144 | public void onTimeSelect(Date date, View v) {
145 | Toast.makeText(MainActivity.this, getTime(date), Toast.LENGTH_SHORT).show();
146 | }
147 | }).build();
148 | ```
149 |
150 | ```java
151 | //条件选择器
152 | OptionsPickerView pvOptions = new OptionsPickerBuilder(MainActivity.this, new OnOptionsSelectListener() {
153 | @Override
154 | public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
155 | //返回的分别是三个级别的选中位置
156 | String tx = options1Items.get(options1).getPickerViewText()
157 | + options2Items.get(options1).get(option2)
158 | + options3Items.get(options1).get(option2).get(options3).getPickerViewText();
159 | tvOptions.setText(tx);
160 | }
161 | }).build();
162 | pvOptions.setPicker(options1Items, options2Items, options3Items);
163 | pvOptions.show();
164 | ```
165 | #### 大功告成~
166 |
167 | #### 3.如果默认样式不符合你的口味,可以自定义各种属性:
168 | ```java
169 | Calendar selectedDate = Calendar.getInstance();
170 | Calendar startDate = Calendar.getInstance();
171 | //startDate.set(2013,1,1);
172 | Calendar endDate = Calendar.getInstance();
173 | //endDate.set(2020,1,1);
174 |
175 | //正确设置方式 原因:注意事项有说明
176 | startDate.set(2013,0,1);
177 | endDate.set(2020,11,31);
178 |
179 | pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
180 | @Override
181 | public void onTimeSelect(Date date,View v) {//选中事件回调
182 | tvTime.setText(getTime(date));
183 | }
184 | })
185 | .setType(new boolean[]{true, true, true, true, true, true})// 默认全部显示
186 | .setCancelText("Cancel")//取消按钮文字
187 | .setSubmitText("Sure")//确认按钮文字
188 | .setContentSize(18)//滚轮文字大小
189 | .setTitleSize(20)//标题文字大小
190 | .setTitleText("Title")//标题文字
191 | .setOutSideCancelable(false)//点击屏幕,点在控件外部范围时,是否取消显示
192 | .isCyclic(true)//是否循环滚动
193 | .setTitleColor(Color.BLACK)//标题文字颜色
194 | .setSubmitColor(Color.BLUE)//确定按钮文字颜色
195 | .setCancelColor(Color.BLUE)//取消按钮文字颜色
196 | .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
197 | .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
198 | .setDate(selectedDate)// 如果不设置的话,默认是系统时间*/
199 | .setRangDate(startDate,endDate)//起始终止年月日设定
200 | .setLabel("年","月","日","时","分","秒")//默认设置为年月日时分秒
201 | .isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
202 | .isDialog(true)//是否显示为对话框样式
203 | .build();
204 | ```
205 |
206 | ```java
207 | pvOptions = new OptionsPickerBuilder(this, new OptionsPickerView.OnOptionsSelectListener() {
208 | @Override
209 | public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
210 | //返回的分别是三个级别的选中位置
211 | String tx = options1Items.get(options1).getPickerViewText()
212 | + options2Items.get(options1).get(option2)
213 | + options3Items.get(options1).get(option2).get(options3).getPickerViewText();
214 | tvOptions.setText(tx);
215 | }
216 | }) .setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
217 | @Override
218 | public void onOptionsSelectChanged(int options1, int options2, int options3) {
219 | String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
220 | Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
221 | }
222 | })
223 | .setSubmitText("确定")//确定按钮文字
224 | .setCancelText("取消")//取消按钮文字
225 | .setTitleText("城市选择")//标题
226 | .setSubCalSize(18)//确定和取消文字大小
227 | .setTitleSize(20)//标题文字大小
228 | .setTitleColor(Color.BLACK)//标题文字颜色
229 | .setSubmitColor(Color.BLUE)//确定按钮文字颜色
230 | .setCancelColor(Color.BLUE)//取消按钮文字颜色
231 | .setTitleBgColor(0xFF333333)//标题背景颜色 Night mode
232 | .setBgColor(0xFF000000)//滚轮背景颜色 Night mode
233 | .setContentTextSize(18)//滚轮文字大小
234 | .setLinkage(false)//设置是否联动,默认true
235 | .setLabels("省", "市", "区")//设置选择的三级单位
236 | .isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
237 | .setCyclic(false, false, false)//循环与否
238 | .setSelectOptions(1, 1, 1) //设置默认选中项
239 | .setOutSideCancelable(false)//点击外部dismiss default true
240 | .isDialog(true)//是否显示为对话框样式
241 | .isRestoreItem(true)//切换时是否还原,设置默认选中第一项。
242 | .build();
243 |
244 | pvOptions.setPicker(options1Items, options2Items, options3Items);//添加数据源
245 | ```
246 | #### 4.如果需要自定义布局:
247 |
248 | ```java
249 | // 注意:自定义布局中,id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有,否则会报空指针
250 | // 具体可参考demo 里面的两个自定义布局
251 | pvCustomOptions = new OptionsPickerBuilder(this, new OptionsPickerView.OnOptionsSelectListener() {
252 | @Override
253 | public void onOptionsSelect(int options1, int option2, int options3, View v) {
254 | //返回的分别是三个级别的选中位置
255 | String tx = cardItem.get(options1).getPickerViewText();
256 | btn_CustomOptions.setText(tx);
257 | }
258 | })
259 | .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
260 | @Override
261 | public void customLayout(View v) {
262 | //自定义布局中的控件初始化及事件处理
263 | final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
264 | final TextView tvAdd = (TextView) v.findViewById(R.id.tv_add);
265 | ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
266 | tvSubmit.setOnClickListener(new View.OnClickListener() {
267 | @Override
268 | public void onClick(View v) {
269 | pvCustomOptions.returnData(tvSubmit);
270 | }
271 | });
272 | ivCancel.setOnClickListener(new View.OnClickListener() {
273 | @Override
274 | public void onClick(View v) {
275 | pvCustomOptions.dismiss();
276 | }
277 | });
278 |
279 | tvAdd.setOnClickListener(new View.OnClickListener() {
280 | @Override
281 | public void onClick(View v) {
282 | getData();
283 | pvCustomOptions.setPicker(cardItem);
284 | }
285 | });
286 |
287 | }
288 | })
289 | .build();
290 | pvCustomOptions.setPicker(cardItem);//添加数据
291 | ```
292 |
293 | #### 5.对使用还有疑问的话,可参考demo代码
294 | [请戳我查看demo代码](https://github.com/Bigkoo/Android-PickerView/blob/master/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java)
295 |
296 |
297 |
298 | #### 6.若只需要WheelView基础控件自行扩展实现逻辑,可直接添加基础控件库,Gradle 依赖:
299 |
300 | ```java
301 | compile 'com.contrarywind:wheelview:4.1.0'
302 | ```
303 |
304 | #### WheelView 使用代码示例:
305 |
306 | xml布局:
307 | ```xml
308 |
312 | ```
313 |
314 | Java 代码:
315 | ```java
316 | WheelView wheelView = findViewById(R.id.wheelview);
317 |
318 | wheelView.setCyclic(false);
319 |
320 | final List mOptionsItems = new ArrayList<>();
321 | mOptionsItems.add("item0");
322 | mOptionsItems.add("item1");
323 | mOptionsItems.add("item2");
324 |
325 | wheelView.setAdapter(new ArrayWheelAdapter(mOptionsItems));
326 | wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
327 | @Override
328 | public void onItemSelected(int index) {
329 | Toast.makeText(MainActivity.this, "" + mOptionsItems.get(index), Toast.LENGTH_SHORT).show();
330 | }
331 | });
332 | ```
333 |
334 |
335 | ### 效果图(招行信用卡的“掌上生活”里面条件选择器他们用的就是我这个库,大家可以当实际项目参考)
336 | 
337 |
338 |
339 | ### Thanks
340 |
341 | - WheelView
342 | - [androidWheelView](https://github.com/weidongjian/androidWheelView/)
343 |
344 | ## License
345 |
346 | ```
347 | Copyright 2014 Bigkoo
348 | Licensed under the Apache License, Version 2.0 (the "License");
349 | you may not use this file except in compliance with the License.
350 | You may obtain a copy of the License at
351 |
352 | http://www.apache.org/licenses/LICENSE-2.0
353 |
354 | Unless required by applicable law or agreed to in writing, software
355 | distributed under the License is distributed on an "AS IS" BASIS,
356 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
357 | See the License for the specific language governing permissions and
358 | limitations under the License.
359 | ```
360 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 |
6 | defaultConfig {
7 | applicationId "com.bigkoo.pickerviewdemo"
8 | minSdkVersion 14
9 | targetSdkVersion 26
10 | versionCode 1
11 | versionName "1.0"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | lintOptions{
20 | checkReleaseBuilds false
21 | abortOnError false
22 | }
23 | }
24 |
25 | dependencies {
26 | // compile fileTree(dir: 'libs', include: ['*.jar'])
27 | implementation project(':pickerview')
28 | // implementation 'com.contrarywind:Android-PickerView:4.1.9'
29 | // implementation 'com.contrarywind:wheelview:4.1.0'
30 | implementation 'com.android.support:appcompat-v7:26.1.0'
31 | implementation 'com.google.code.gson:gson:2.7'
32 | }
33 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/Sai/Documents/software/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/bigkoo/pickerviewdemo/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/FragmentTestActivity.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo;
2 |
3 |
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.support.v4.app.FragmentManager;
7 | import android.support.v4.app.FragmentTransaction;
8 | import android.support.v7.app.AppCompatActivity;
9 |
10 | public class FragmentTestActivity extends AppCompatActivity {
11 | private FragmentManager mFragmentManager;
12 |
13 | @Override
14 | protected void onCreate(@Nullable Bundle savedInstanceState) {
15 | super.onCreate(savedInstanceState);
16 | setContentView(R.layout.activity_fragmenttest);
17 | mFragmentManager = getSupportFragmentManager();
18 | FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
19 | fragmentTransaction.replace(R.id.frame_activity_main, new TestFragment());
20 | fragmentTransaction.commitAllowingStateLoss();
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/GetJsonDataUtil.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo;
2 |
3 | import android.content.Context;
4 | import android.content.res.AssetManager;
5 |
6 | import java.io.BufferedReader;
7 | import java.io.IOException;
8 | import java.io.InputStreamReader;
9 |
10 | /**
11 | * <读取Json文件的工具类>
12 | *
13 | * @author: 小嵩
14 | * @date: 2017/3/16 16:22
15 |
16 | */
17 |
18 | public class GetJsonDataUtil {
19 |
20 |
21 | public String getJson(Context context,String fileName) {
22 |
23 | StringBuilder stringBuilder = new StringBuilder();
24 | try {
25 | AssetManager assetManager = context.getAssets();
26 | BufferedReader bf = new BufferedReader(new InputStreamReader(
27 | assetManager.open(fileName)));
28 | String line;
29 | while ((line = bf.readLine()) != null) {
30 | stringBuilder.append(line);
31 | }
32 | } catch (IOException e) {
33 | e.printStackTrace();
34 | }
35 | return stringBuilder.toString();
36 | }
37 | }
38 |
39 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/JsonDataActivity.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo;
2 |
3 | import android.annotation.SuppressLint;
4 | import android.graphics.Color;
5 | import android.os.Bundle;
6 | import android.os.Handler;
7 | import android.os.Message;
8 | import android.support.v7.app.AppCompatActivity;
9 | import android.view.View;
10 | import android.widget.Toast;
11 |
12 | import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
13 | import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
14 | import com.bigkoo.pickerview.view.OptionsPickerView;
15 | import com.bigkoo.pickerviewdemo.bean.JsonBean;
16 | import com.google.gson.Gson;
17 |
18 | import org.json.JSONArray;
19 |
20 | import java.util.ArrayList;
21 | import java.util.List;
22 |
23 | /**
24 | * 解析省市区数据示例
25 | *
26 | * @author 小嵩
27 | * @date 2017-3-16
28 | */
29 | public class JsonDataActivity extends AppCompatActivity implements View.OnClickListener {
30 |
31 |
32 | private List options1Items = new ArrayList<>();
33 | private ArrayList> options2Items = new ArrayList<>();
34 | private ArrayList>> options3Items = new ArrayList<>();
35 | private Thread thread;
36 | private static final int MSG_LOAD_DATA = 0x0001;
37 | private static final int MSG_LOAD_SUCCESS = 0x0002;
38 | private static final int MSG_LOAD_FAILED = 0x0003;
39 |
40 | private static boolean isLoaded = false;
41 |
42 | @Override
43 | protected void onCreate(Bundle savedInstanceState) {
44 | super.onCreate(savedInstanceState);
45 | setContentView(R.layout.activity_json_data);
46 | initView();
47 | }
48 |
49 | @SuppressLint("HandlerLeak")
50 | private Handler mHandler = new Handler() {
51 | public void handleMessage(Message msg) {
52 | switch (msg.what) {
53 | case MSG_LOAD_DATA:
54 | if (thread == null) {//如果已创建就不再重新创建子线程了
55 |
56 | Toast.makeText(JsonDataActivity.this, "Begin Parse Data", Toast.LENGTH_SHORT).show();
57 | thread = new Thread(new Runnable() {
58 | @Override
59 | public void run() {
60 | // 子线程中解析省市区数据
61 | initJsonData();
62 | }
63 | });
64 | thread.start();
65 | }
66 | break;
67 |
68 | case MSG_LOAD_SUCCESS:
69 | Toast.makeText(JsonDataActivity.this, "Parse Succeed", Toast.LENGTH_SHORT).show();
70 | isLoaded = true;
71 | break;
72 |
73 | case MSG_LOAD_FAILED:
74 | Toast.makeText(JsonDataActivity.this, "Parse Failed", Toast.LENGTH_SHORT).show();
75 | break;
76 | }
77 | }
78 | };
79 |
80 | private void initView() {
81 | findViewById(R.id.btn_data).setOnClickListener(this);
82 | findViewById(R.id.btn_show).setOnClickListener(this);
83 | }
84 |
85 | @Override
86 | public void onClick(View v) {
87 | switch (v.getId()) {
88 | case R.id.btn_data:
89 | mHandler.sendEmptyMessage(MSG_LOAD_DATA);
90 | break;
91 | case R.id.btn_show:
92 | if (isLoaded) {
93 | showPickerView();
94 | } else {
95 | Toast.makeText(JsonDataActivity.this, "Please waiting until the data is parsed", Toast.LENGTH_SHORT).show();
96 | }
97 | break;
98 | }
99 | }
100 |
101 |
102 | private void showPickerView() {// 弹出选择器
103 |
104 | OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
105 | @Override
106 | public void onOptionsSelect(int options1, int options2, int options3, View v) {
107 | //返回的分别是三个级别的选中位置
108 | String opt1tx = options1Items.size() > 0 ?
109 | options1Items.get(options1).getPickerViewText() : "";
110 |
111 | String opt2tx = options2Items.size() > 0
112 | && options2Items.get(options1).size() > 0 ?
113 | options2Items.get(options1).get(options2) : "";
114 |
115 | String opt3tx = options2Items.size() > 0
116 | && options3Items.get(options1).size() > 0
117 | && options3Items.get(options1).get(options2).size() > 0 ?
118 | options3Items.get(options1).get(options2).get(options3) : "";
119 |
120 | String tx = opt1tx + opt2tx + opt3tx;
121 | Toast.makeText(JsonDataActivity.this, tx, Toast.LENGTH_SHORT).show();
122 | }
123 | })
124 |
125 | .setTitleText("城市选择")
126 | .setDividerColor(Color.BLACK)
127 | .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
128 | .setContentTextSize(20)
129 | .build();
130 |
131 | /*pvOptions.setPicker(options1Items);//一级选择器
132 | pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
133 | pvOptions.setPicker(options1Items, options2Items, options3Items);//三级选择器
134 | pvOptions.show();
135 | }
136 |
137 | private void initJsonData() {//解析数据
138 |
139 | /**
140 | * 注意:assets 目录下的Json文件仅供参考,实际使用可自行替换文件
141 | * 关键逻辑在于循环体
142 | *
143 | * */
144 | String JsonData = new GetJsonDataUtil().getJson(this, "province.json");//获取assets目录下的json文件数据
145 |
146 | ArrayList jsonBean = parseData(JsonData);//用Gson 转成实体
147 |
148 | /**
149 | * 添加省份数据
150 | *
151 | * 注意:如果是添加的JavaBean实体,则实体类需要实现 IPickerViewData 接口,
152 | * PickerView会通过getPickerViewText方法获取字符串显示出来。
153 | */
154 | options1Items = jsonBean;
155 |
156 | for (int i = 0; i < jsonBean.size(); i++) {//遍历省份
157 | ArrayList cityList = new ArrayList<>();//该省的城市列表(第二级)
158 | ArrayList> province_AreaList = new ArrayList<>();//该省的所有地区列表(第三极)
159 |
160 | for (int c = 0; c < jsonBean.get(i).getCityList().size(); c++) {//遍历该省份的所有城市
161 | String cityName = jsonBean.get(i).getCityList().get(c).getName();
162 | cityList.add(cityName);//添加城市
163 | ArrayList city_AreaList = new ArrayList<>();//该城市的所有地区列表
164 |
165 | //如果无地区数据,建议添加空字符串,防止数据为null 导致三个选项长度不匹配造成崩溃
166 | /*if (jsonBean.get(i).getCityList().get(c).getArea() == null
167 | || jsonBean.get(i).getCityList().get(c).getArea().size() == 0) {
168 | city_AreaList.add("");
169 | } else {
170 | city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
171 | }*/
172 | city_AreaList.addAll(jsonBean.get(i).getCityList().get(c).getArea());
173 | province_AreaList.add(city_AreaList);//添加该省所有地区数据
174 | }
175 |
176 | /**
177 | * 添加城市数据
178 | */
179 | options2Items.add(cityList);
180 |
181 | /**
182 | * 添加地区数据
183 | */
184 | options3Items.add(province_AreaList);
185 | }
186 |
187 | mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);
188 |
189 | }
190 |
191 |
192 | public ArrayList parseData(String result) {//Gson 解析
193 | ArrayList detail = new ArrayList<>();
194 | try {
195 | JSONArray data = new JSONArray(result);
196 | Gson gson = new Gson();
197 | for (int i = 0; i < data.length(); i++) {
198 | JsonBean entity = gson.fromJson(data.optJSONObject(i).toString(), JsonBean.class);
199 | detail.add(entity);
200 | }
201 | } catch (Exception e) {
202 | e.printStackTrace();
203 | mHandler.sendEmptyMessage(MSG_LOAD_FAILED);
204 | }
205 | return detail;
206 | }
207 |
208 |
209 | @Override
210 | protected void onDestroy() {
211 | super.onDestroy();
212 | if (mHandler != null) {
213 | mHandler.removeCallbacksAndMessages(null);
214 | }
215 | }
216 | }
217 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/TestCircleWheelViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 | import android.widget.Toast;
7 |
8 | import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
9 | import com.contrarywind.listener.OnItemSelectedListener;
10 | import com.contrarywind.view.WheelView;
11 |
12 | import java.util.ArrayList;
13 | import java.util.List;
14 |
15 | /**
16 | * desc:
17 | * author: Created by lixiaotong on 2019-07-30
18 | * e-mail: 516030811@qq.com
19 | */
20 | public class TestCircleWheelViewActivity extends AppCompatActivity {
21 |
22 | @Override
23 | protected void onCreate(@Nullable Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_test_circle_wheelview);
26 |
27 | WheelView wheelView = findViewById(R.id.wheelview);
28 | wheelView.setTextSize(20);
29 | wheelView.setLineSpacingMultiplier(2f);
30 | // wheelView.setDividerWidth(6);
31 | wheelView.setDividerType(WheelView.DividerType.CIRCLE);
32 |
33 | final List mOptionsItems = new ArrayList<>();
34 | mOptionsItems.add("10");
35 | mOptionsItems.add("20");
36 | mOptionsItems.add("30");
37 | mOptionsItems.add("40");
38 | mOptionsItems.add("50");
39 | mOptionsItems.add("60");
40 | mOptionsItems.add("70");
41 |
42 | wheelView.setAdapter(new ArrayWheelAdapter(mOptionsItems));
43 | wheelView.setOnItemSelectedListener(new OnItemSelectedListener() {
44 | @Override
45 | public void onItemSelected(int index) {
46 | Toast.makeText(TestCircleWheelViewActivity.this, "" + mOptionsItems.get(index), Toast.LENGTH_SHORT).show();
47 | }
48 | });
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/TestFragment.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo;
2 |
3 |
4 | import android.graphics.Color;
5 | import android.os.Bundle;
6 | import android.support.annotation.Nullable;
7 | import android.support.v4.app.Fragment;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.view.ViewGroup;
11 | import android.widget.Button;
12 | import android.widget.FrameLayout;
13 | import android.widget.ImageView;
14 | import android.widget.TextView;
15 |
16 | import com.bigkoo.pickerview.builder.TimePickerBuilder;
17 | import com.bigkoo.pickerview.listener.CustomListener;
18 | import com.bigkoo.pickerview.listener.OnTimeSelectListener;
19 | import com.bigkoo.pickerview.view.TimePickerView;
20 |
21 | import java.text.SimpleDateFormat;
22 | import java.util.Calendar;
23 | import java.util.Date;
24 |
25 | public class TestFragment extends Fragment implements View.OnClickListener {
26 | private View mView;
27 | private Button btnShow;
28 | private TimePickerView pvTime;
29 | private FrameLayout mFrameLayout;
30 |
31 | @Nullable
32 | @Override
33 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
34 | mView = inflater.inflate(R.layout.fragment_test, null);
35 | return mView;
36 | }
37 |
38 |
39 | @Override
40 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
41 | super.onActivityCreated(savedInstanceState);
42 | btnShow = (Button) mView.findViewById(R.id.btn_show);
43 | btnShow.setOnClickListener(this);
44 | mFrameLayout = (FrameLayout) mView.findViewById(R.id.fragmen_fragment);
45 | initTimePicker();
46 | }
47 |
48 | private void initTimePicker() {
49 | //控制时间范围(如果不设置范围,则使用默认时间1900-2100年,此段代码可注释)
50 | //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
51 | Calendar selectedDate = Calendar.getInstance();
52 |
53 | Calendar startDate = Calendar.getInstance();
54 | startDate.set(2013, 0, 23);
55 |
56 | Calendar endDate = Calendar.getInstance();
57 | endDate.set(2019, 11, 28);
58 | //时间选择器
59 | pvTime = new TimePickerBuilder(getActivity(), new OnTimeSelectListener() {
60 | @Override
61 | public void onTimeSelect(Date date, View v) {//选中事件回调
62 | // 这里回调过来的v,就是show()方法里面所添加的 View 参数,如果show的时候没有添加参数,v则为null
63 | /*btn_Time.setText(getTime(date));*/
64 | Button btn = (Button) v;
65 | btn.setText(getTime(date));
66 | }
67 | })
68 | .setLayoutRes(R.layout.pickerview_custom_time, new CustomListener() {
69 |
70 | @Override
71 | public void customLayout(View v) {
72 | final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
73 | ImageView ivCancel = (ImageView) v.findViewById(R.id.iv_cancel);
74 | tvSubmit.setOnClickListener(new View.OnClickListener() {
75 | @Override
76 | public void onClick(View v) {
77 | pvTime.returnData();
78 | /*pvTime.dismiss();*/
79 | }
80 | });
81 | ivCancel.setOnClickListener(new View.OnClickListener() {
82 | @Override
83 | public void onClick(View v) {
84 | /*pvTime.dismiss();*/
85 | }
86 | });
87 | }
88 | })
89 | .setType(new boolean[]{true, true, true, false, false, false})
90 | .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示 hide label
91 | .setDividerColor(Color.DKGRAY)
92 | .setContentTextSize(20)
93 | .setDate(selectedDate)
94 | .setRangDate(startDate, selectedDate)
95 | .setDecorView(mFrameLayout)//非dialog模式下,设置ViewGroup, pickerView将会添加到这个ViewGroup中
96 | .setOutSideColor(0x00000000)
97 | .setOutSideCancelable(false)
98 | .build();
99 |
100 | pvTime.setKeyBackCancelable(false);//系统返回键监听屏蔽掉
101 | }
102 |
103 | @Override
104 | public void onClick(View v) {
105 | pvTime.show(v, false);//弹出时间选择器,传递参数过去,回调的时候则可以绑定此view
106 | }
107 |
108 | private String getTime(Date date) {//可根据需要自行截取数据显示
109 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
110 | return format.format(date);
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/bean/CardBean.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo.bean;
2 |
3 |
4 | import com.contrarywind.interfaces.IPickerViewData;
5 |
6 | /**
7 | * Created by KyuYi on 2017/3/2.
8 | * E-Mail:kyu_yi@sina.com
9 | * 功能:
10 | */
11 |
12 | public class CardBean implements IPickerViewData {
13 | int id;
14 | String cardNo;
15 |
16 | public CardBean(int id, String cardNo) {
17 | this.id = id;
18 | this.cardNo = cardNo;
19 | }
20 |
21 | public int getId() {
22 | return id;
23 | }
24 |
25 | public void setId(int id) {
26 | this.id = id;
27 | }
28 |
29 | public String getCardNo() {
30 | return cardNo;
31 | }
32 |
33 | public void setCardNo(String cardNo) {
34 | this.cardNo = cardNo;
35 | }
36 |
37 | @Override
38 | public String getPickerViewText() {
39 | return cardNo;
40 | }
41 |
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/bean/JsonBean.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo.bean;
2 |
3 |
4 | import com.contrarywind.interfaces.IPickerViewData;
5 |
6 | import java.util.List;
7 |
8 | /**
9 | * TODO
10 | *
11 | * @author: 小嵩
12 | * @date: 2017/3/16 15:36
13 | */
14 |
15 | public class JsonBean implements IPickerViewData {
16 |
17 |
18 | /**
19 | * name : 省份
20 | * city : [{"name":"北京市","area":["东城区","西城区","崇文区","宣武区","朝阳区"]}]
21 | */
22 |
23 | private String name;
24 | private List city;
25 |
26 | public String getName() {
27 | return name;
28 | }
29 |
30 | public void setName(String name) {
31 | this.name = name;
32 | }
33 |
34 | public List getCityList() {
35 | return city;
36 | }
37 |
38 | public void setCityList(List city) {
39 | this.city = city;
40 | }
41 |
42 | // 实现 IPickerViewData 接口,
43 | // 这个用来显示在PickerView上面的字符串,
44 | // PickerView会通过IPickerViewData获取getPickerViewText方法显示出来。
45 | @Override
46 | public String getPickerViewText() {
47 | return this.name;
48 | }
49 |
50 |
51 | public static class CityBean {
52 | /**
53 | * name : 城市
54 | * area : ["东城区","西城区","崇文区","昌平区"]
55 | */
56 |
57 | private String name;
58 | private List area;
59 |
60 | public String getName() {
61 | return name;
62 | }
63 |
64 | public void setName(String name) {
65 | this.name = name;
66 | }
67 |
68 | public List getArea() {
69 | return area;
70 | }
71 |
72 | public void setArea(List area) {
73 | this.area = area;
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/app/src/main/java/com/bigkoo/pickerviewdemo/bean/ProvinceBean.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerviewdemo.bean;
2 |
3 |
4 | import com.contrarywind.interfaces.IPickerViewData;
5 |
6 | /**
7 | * Created by Sai on 15/11/22.
8 | */
9 | public class ProvinceBean implements IPickerViewData {
10 | private long id;
11 | private String name;
12 | private String description;
13 | private String others;
14 |
15 | public ProvinceBean(long id,String name,String description,String others){
16 | this.id = id;
17 | this.name = name;
18 | this.description = description;
19 | this.others = others;
20 | }
21 |
22 | public long getId() {
23 | return id;
24 | }
25 |
26 | public void setId(long id) {
27 | this.id = id;
28 | }
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public void setName(String name) {
35 | this.name = name;
36 | }
37 |
38 | public String getDescription() {
39 | return description;
40 | }
41 |
42 | public void setDescription(String description) {
43 | this.description = description;
44 | }
45 |
46 | public String getOthers() {
47 | return others;
48 | }
49 |
50 | public void setOthers(String others) {
51 | this.others = others;
52 | }
53 |
54 | //这个用来显示在PickerView上面的字符串,PickerView会通过getPickerViewText方法获取字符串显示出来。
55 | @Override
56 | public String getPickerViewText() {
57 | return name;
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/app/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/app/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/app/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/to_down.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/app/src/main/res/drawable-xhdpi/to_down.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/app/src/main/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_fragmenttest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_json_data.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
18 |
19 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
11 |
12 |
16 |
17 |
18 |
24 |
25 |
31 |
32 |
39 |
40 |
47 |
48 |
55 |
56 |
63 |
64 |
71 |
72 |
79 |
80 |
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_test_circle_wheelview.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_test.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
15 |
16 |
17 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/pickerview_custom_lunar.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
16 |
17 |
25 |
26 |
35 |
36 |
47 |
48 |
52 |
53 |
54 |
55 |
56 |
64 |
65 |
70 |
71 |
77 |
78 |
83 |
84 |
89 |
90 |
95 |
96 |
101 |
102 |
103 |
104 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/pickerview_custom_options.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
16 |
17 |
25 |
26 |
37 |
38 |
42 |
43 |
44 |
45 |
53 |
54 |
59 |
60 |
65 |
66 |
71 |
72 |
73 |
77 |
78 |
87 |
88 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/pickerview_custom_time.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
16 |
17 |
25 |
26 |
37 |
38 |
42 |
43 |
44 |
45 |
46 |
54 |
55 |
60 |
61 |
67 |
68 |
73 |
74 |
79 |
80 |
85 |
86 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values-en/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | LunarTimePicker
4 |
5 | TimePicker
6 | OptionsPicker
7 |
8 | CustomLayoutTimePicker
9 | CustomLayoutOptionsPicker
10 | CustomParentView
11 |
12 | OptionsPicker(Non-Linkage)
13 | AreaDataParseSample
14 |
15 | Parse Data
16 | Show OptionsPicker
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | PickerViewDemo
5 | Hello world!
6 | Settings
7 |
8 |
9 |
10 | 解析数据
11 | 显示省市区选择器
12 |
13 | 公农历切换
14 | 时间选择器
15 | 条件选择器
16 |
17 | 时间选择器自定义布局
18 | 条件选择器自定义布局
19 | 自定义父容器
20 | 使用圆形分割线
21 |
22 | 条件选择器(不联动)
23 |
24 | 省市区Json文件解析示例
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | maven {
6 | url 'https://maven.google.com/'
7 | name 'Google'
8 | }
9 | jcenter()
10 | }
11 | dependencies {
12 | classpath 'com.android.tools.build:gradle:3.5.1'
13 | // classpath "com.novoda:bintray-release:0.9.1"
14 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
15 | // NOTE: Do not place your application dependencies here; they belong
16 | // in the individual module build.gradle files
17 | }
18 | }
19 |
20 | allprojects {
21 | repositories {
22 | jcenter()
23 | maven {
24 | url 'https://maven.google.com/'
25 | name 'Google'
26 | }
27 | }
28 | }
29 |
30 | task clean(type: Delete) {
31 | delete rootProject.buildDir
32 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m
13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
14 |
15 | # When configured, Gradle will run in incubating parallel mode.
16 | # This option should only be used with decoupled projects. More details, visit
17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
18 | # org.gradle.parallel=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Oct 14 15:15:59 CST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/pickerview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/pickerview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | //apply plugin: 'com.novoda.bintray-release'//添加JCenter插件
4 |
5 |
6 |
7 | android {
8 | compileSdkVersion 26
9 |
10 | defaultConfig {
11 | minSdkVersion 14
12 | targetSdkVersion 26
13 | versionCode 34
14 | versionName "4.1.9"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | lintOptions {
23 | abortOnError false
24 | }
25 | }
26 |
27 | //allprojects {
28 | // tasks.withType(Javadoc) {//兼容中文字符
29 | // options{
30 | // encoding "UTF-8"
31 | // charSet 'UTF-8'
32 | // links "http://docs.oracle.com/javase/7/docs/api"
33 | // }
34 | // }
35 | //}
36 | //publish {
37 | // userOrg = 'contrarywind'//bintray.com 用户名/组织名 user/org name
38 | // groupId = 'com.contrarywind'//JCenter上显示的路径 path
39 | // artifactId = 'Android-PickerView'//项目名称 project name
40 | // publishVersion = '4.1.9'//版本号 version code
41 | // desc = 'this is a pickerview for android'//项目描述 description
42 | // website = 'https://github.com/Bigkoo/Android-PickerView' //项目网址链接 link
43 | //}
44 |
45 | dependencies {
46 | // compile fileTree(include: ['*.jar'], dir: 'libs')
47 | api project(path: ':wheelview')
48 | // api 'com.contrarywind:wheelview:4.1.0'
49 | implementation 'com.android.support:support-annotations:28.0.0'
50 | }
--------------------------------------------------------------------------------
/pickerview/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in /Users/Sai/Documents/software/sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/pickerview/src/androidTest/java/com/bigkoo/pickerview/ApplicationTest.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview;
2 |
3 | import android.app.Application;
4 | import android.test.ApplicationTestCase;
5 |
6 | /**
7 | * Testing Fundamentals
8 | */
9 | public class ApplicationTest extends ApplicationTestCase {
10 | public ApplicationTest() {
11 | super(Application.class);
12 | }
13 | }
--------------------------------------------------------------------------------
/pickerview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/adapter/ArrayWheelAdapter.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.adapter;
2 |
3 | import com.contrarywind.adapter.WheelAdapter;
4 |
5 | import java.util.List;
6 |
7 | /**
8 | * The simple Array wheel adapter
9 | * @param the element type
10 | */
11 | public class ArrayWheelAdapter implements WheelAdapter {
12 |
13 |
14 | // items
15 | private List items;
16 |
17 | /**
18 | * Constructor
19 | * @param items the items
20 | */
21 | public ArrayWheelAdapter(List items) {
22 | this.items = items;
23 |
24 | }
25 |
26 | @Override
27 | public Object getItem(int index) {
28 | if (index >= 0 && index < items.size()) {
29 | return items.get(index);
30 | }
31 | return "";
32 | }
33 |
34 | @Override
35 | public int getItemsCount() {
36 | return items.size();
37 | }
38 |
39 | @Override
40 | public int indexOf(Object o){
41 | return items.indexOf(o);
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/adapter/NumericWheelAdapter.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.adapter;
2 |
3 |
4 | import com.contrarywind.adapter.WheelAdapter;
5 |
6 | /**
7 | * Numeric Wheel adapter.
8 | */
9 | public class NumericWheelAdapter implements WheelAdapter {
10 |
11 | private int minValue;
12 | private int maxValue;
13 |
14 | /**
15 | * Constructor
16 | * @param minValue the wheel min value
17 | * @param maxValue the wheel max value
18 | */
19 | public NumericWheelAdapter(int minValue, int maxValue) {
20 | this.minValue = minValue;
21 | this.maxValue = maxValue;
22 | }
23 |
24 | @Override
25 | public Object getItem(int index) {
26 | if (index >= 0 && index < getItemsCount()) {
27 | int value = minValue + index;
28 | return value;
29 | }
30 | return 0;
31 | }
32 |
33 | @Override
34 | public int getItemsCount() {
35 | return maxValue - minValue + 1;
36 | }
37 |
38 | @Override
39 | public int indexOf(Object o){
40 | try {
41 | return (int)o - minValue;
42 | } catch (Exception e) {
43 | return -1;
44 | }
45 |
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/builder/OptionsPickerBuilder.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.builder;
2 |
3 | import android.content.Context;
4 | import android.graphics.Typeface;
5 | import android.support.annotation.ColorInt;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 | import com.bigkoo.pickerview.configure.PickerOptions;
10 | import com.bigkoo.pickerview.listener.CustomListener;
11 | import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
12 | import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
13 | import com.bigkoo.pickerview.view.OptionsPickerView;
14 | import com.contrarywind.view.WheelView;
15 |
16 | /**
17 | * Created by xiaosongzeem on 2018/3/20.
18 | */
19 |
20 | public class OptionsPickerBuilder {
21 |
22 | //配置类
23 | private PickerOptions mPickerOptions;
24 |
25 |
26 | //Required
27 | public OptionsPickerBuilder(Context context, OnOptionsSelectListener listener) {
28 | mPickerOptions = new PickerOptions(PickerOptions.TYPE_PICKER_OPTIONS);
29 | mPickerOptions.context = context;
30 | mPickerOptions.optionsSelectListener = listener;
31 | }
32 |
33 | //Option
34 | public OptionsPickerBuilder setSubmitText(String textContentConfirm) {
35 | mPickerOptions.textContentConfirm = textContentConfirm;
36 | return this;
37 | }
38 |
39 | public OptionsPickerBuilder setCancelText(String textContentCancel) {
40 | mPickerOptions.textContentCancel = textContentCancel;
41 | return this;
42 | }
43 |
44 | public OptionsPickerBuilder setTitleText(String textContentTitle) {
45 | mPickerOptions.textContentTitle = textContentTitle;
46 | return this;
47 | }
48 |
49 | public OptionsPickerBuilder isDialog(boolean isDialog) {
50 | mPickerOptions.isDialog = isDialog;
51 | return this;
52 | }
53 |
54 | public OptionsPickerBuilder addOnCancelClickListener(View.OnClickListener cancelListener) {
55 | mPickerOptions.cancelListener = cancelListener;
56 | return this;
57 | }
58 |
59 |
60 | public OptionsPickerBuilder setSubmitColor(int textColorConfirm) {
61 | mPickerOptions.textColorConfirm = textColorConfirm;
62 | return this;
63 | }
64 |
65 | public OptionsPickerBuilder setCancelColor(int textColorCancel) {
66 | mPickerOptions.textColorCancel = textColorCancel;
67 | return this;
68 | }
69 |
70 |
71 | /**
72 | * {@link #setOutSideColor} instead.
73 | *
74 | * @param backgroundId color resId.
75 | */
76 | @Deprecated
77 | public OptionsPickerBuilder setBackgroundId(int backgroundId) {
78 | mPickerOptions.outSideColor = backgroundId;
79 | return this;
80 | }
81 |
82 | /**
83 | * 显示时的外部背景色颜色,默认是灰色
84 | *
85 | * @param outSideColor color resId.
86 | * @return
87 | */
88 | public OptionsPickerBuilder setOutSideColor(int outSideColor) {
89 | mPickerOptions.outSideColor = outSideColor;
90 | return this;
91 | }
92 |
93 | /**
94 | * ViewGroup 类型
95 | * 设置PickerView的显示容器
96 | *
97 | * @param decorView Parent View.
98 | * @return
99 | */
100 | public OptionsPickerBuilder setDecorView(ViewGroup decorView) {
101 | mPickerOptions.decorView = decorView;
102 | return this;
103 | }
104 |
105 | public OptionsPickerBuilder setLayoutRes(int res, CustomListener listener) {
106 | mPickerOptions.layoutRes = res;
107 | mPickerOptions.customListener = listener;
108 | return this;
109 | }
110 |
111 | public OptionsPickerBuilder setBgColor(int bgColorWheel) {
112 | mPickerOptions.bgColorWheel = bgColorWheel;
113 | return this;
114 | }
115 |
116 | public OptionsPickerBuilder setTitleBgColor(int bgColorTitle) {
117 | mPickerOptions.bgColorTitle = bgColorTitle;
118 | return this;
119 | }
120 |
121 | public OptionsPickerBuilder setTitleColor(int textColorTitle) {
122 | mPickerOptions.textColorTitle = textColorTitle;
123 | return this;
124 | }
125 |
126 | public OptionsPickerBuilder setSubCalSize(int textSizeSubmitCancel) {
127 | mPickerOptions.textSizeSubmitCancel = textSizeSubmitCancel;
128 | return this;
129 | }
130 |
131 | public OptionsPickerBuilder setTitleSize(int textSizeTitle) {
132 | mPickerOptions.textSizeTitle = textSizeTitle;
133 | return this;
134 | }
135 |
136 | public OptionsPickerBuilder setContentTextSize(int textSizeContent) {
137 | mPickerOptions.textSizeContent = textSizeContent;
138 | return this;
139 | }
140 |
141 | public OptionsPickerBuilder setOutSideCancelable(boolean cancelable) {
142 | mPickerOptions.cancelable = cancelable;
143 | return this;
144 | }
145 |
146 |
147 | public OptionsPickerBuilder setLabels(String label1, String label2, String label3) {
148 | mPickerOptions.label1 = label1;
149 | mPickerOptions.label2 = label2;
150 | mPickerOptions.label3 = label3;
151 | return this;
152 | }
153 |
154 | /**
155 | * 设置Item 的间距倍数,用于控制 Item 高度间隔
156 | *
157 | * @param lineSpacingMultiplier 浮点型,1.0-4.0f 之间有效,超过则取极值。
158 | */
159 | public OptionsPickerBuilder setLineSpacingMultiplier(float lineSpacingMultiplier) {
160 | mPickerOptions.lineSpacingMultiplier = lineSpacingMultiplier;
161 | return this;
162 | }
163 |
164 | /**
165 | * Set item divider line type color.
166 | *
167 | * @param dividerColor color resId.
168 | */
169 | public OptionsPickerBuilder setDividerColor(@ColorInt int dividerColor) {
170 | mPickerOptions.dividerColor = dividerColor;
171 | return this;
172 | }
173 |
174 | /**
175 | * Set item divider line type.
176 | *
177 | * @param dividerType enum Type {@link WheelView.DividerType}
178 | */
179 | public OptionsPickerBuilder setDividerType(WheelView.DividerType dividerType) {
180 | mPickerOptions.dividerType = dividerType;
181 | return this;
182 | }
183 |
184 | /**
185 | * Set the textColor of selected item.
186 | *
187 | * @param textColorCenter color res.
188 | */
189 | public OptionsPickerBuilder setTextColorCenter(int textColorCenter) {
190 | mPickerOptions.textColorCenter = textColorCenter;
191 | return this;
192 | }
193 |
194 | /**
195 | * Set the textColor of outside item.
196 | *
197 | * @param textColorOut color resId.
198 | */
199 | public OptionsPickerBuilder setTextColorOut(@ColorInt int textColorOut) {
200 | mPickerOptions.textColorOut = textColorOut;
201 | return this;
202 | }
203 |
204 | public OptionsPickerBuilder setTypeface(Typeface font) {
205 | mPickerOptions.font = font;
206 | return this;
207 | }
208 |
209 | public OptionsPickerBuilder setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
210 | mPickerOptions.cyclic1 = cyclic1;
211 | mPickerOptions.cyclic2 = cyclic2;
212 | mPickerOptions.cyclic3 = cyclic3;
213 | return this;
214 | }
215 |
216 | public OptionsPickerBuilder setSelectOptions(int option1) {
217 | mPickerOptions.option1 = option1;
218 | return this;
219 | }
220 |
221 | public OptionsPickerBuilder setSelectOptions(int option1, int option2) {
222 | mPickerOptions.option1 = option1;
223 | mPickerOptions.option2 = option2;
224 | return this;
225 | }
226 |
227 | public OptionsPickerBuilder setSelectOptions(int option1, int option2, int option3) {
228 | mPickerOptions.option1 = option1;
229 | mPickerOptions.option2 = option2;
230 | mPickerOptions.option3 = option3;
231 | return this;
232 | }
233 |
234 | public OptionsPickerBuilder setTextXOffset(int xoffset_one, int xoffset_two, int xoffset_three) {
235 | mPickerOptions.x_offset_one = xoffset_one;
236 | mPickerOptions.x_offset_two = xoffset_two;
237 | mPickerOptions.x_offset_three = xoffset_three;
238 | return this;
239 | }
240 |
241 | public OptionsPickerBuilder isCenterLabel(boolean isCenterLabel) {
242 | mPickerOptions.isCenterLabel = isCenterLabel;
243 | return this;
244 | }
245 |
246 |
247 | /**
248 | * 设置最大可见数目
249 | *
250 | * @param count 建议设置为 3 ~ 9之间。
251 | */
252 | public OptionsPickerBuilder setItemVisibleCount(int count) {
253 | mPickerOptions.itemsVisibleCount = count;
254 | return this;
255 | }
256 |
257 | /**
258 | * 透明度是否渐变
259 | *
260 | * @param isAlphaGradient true of false
261 | */
262 | public OptionsPickerBuilder isAlphaGradient(boolean isAlphaGradient) {
263 | mPickerOptions.isAlphaGradient = isAlphaGradient;
264 | return this;
265 | }
266 |
267 | /**
268 | * 切换选项时,是否还原第一项
269 | *
270 | * @param isRestoreItem true:还原; false: 保持上一个选项
271 | * @return TimePickerBuilder
272 | */
273 | public OptionsPickerBuilder isRestoreItem(boolean isRestoreItem) {
274 | mPickerOptions.isRestoreItem = isRestoreItem;
275 | return this;
276 | }
277 |
278 | /**
279 | * @param listener 切换item项滚动停止时,实时回调监听。
280 | * @return
281 | */
282 | public OptionsPickerBuilder setOptionsSelectChangeListener(OnOptionsSelectChangeListener listener) {
283 | mPickerOptions.optionsSelectChangeListener = listener;
284 | return this;
285 | }
286 |
287 |
288 | public OptionsPickerView build() {
289 | return new OptionsPickerView<>(mPickerOptions);
290 | }
291 | }
292 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.builder;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.ColorInt;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 |
8 | import com.bigkoo.pickerview.configure.PickerOptions;
9 | import com.bigkoo.pickerview.listener.CustomListener;
10 | import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
11 | import com.bigkoo.pickerview.listener.OnTimeSelectListener;
12 | import com.bigkoo.pickerview.view.TimePickerView;
13 | import com.contrarywind.view.WheelView;
14 |
15 | import java.util.Calendar;
16 | import java.util.List;
17 |
18 | /**
19 | * Created by xiaosongzeem on 2018/3/20.
20 | */
21 |
22 | public class TimePickerBuilder {
23 |
24 | private PickerOptions mPickerOptions;
25 |
26 | //Required
27 | public TimePickerBuilder(Context context, OnTimeSelectListener listener) {
28 | mPickerOptions = new PickerOptions(PickerOptions.TYPE_PICKER_TIME);
29 | mPickerOptions.context = context;
30 | mPickerOptions.timeSelectListener = listener;
31 | }
32 |
33 | //Option
34 | public TimePickerBuilder setGravity(int gravity) {
35 | mPickerOptions.textGravity = gravity;
36 | return this;
37 | }
38 |
39 | public TimePickerBuilder addOnCancelClickListener(View.OnClickListener cancelListener) {
40 | mPickerOptions.cancelListener = cancelListener;
41 | return this;
42 | }
43 |
44 | /**
45 | * new boolean[]{true, true, true, false, false, false}
46 | * control the "year","month","day","hours","minutes","seconds " display or hide.
47 | * 分别控制“年”“月”“日”“时”“分”“秒”的显示或隐藏。
48 | *
49 | * @param type 布尔型数组,长度需要设置为6。
50 | * @return TimePickerBuilder
51 | */
52 | public TimePickerBuilder setType(boolean[] type) {
53 | mPickerOptions.type = type;
54 | return this;
55 | }
56 |
57 | public TimePickerBuilder setSubmitText(String textContentConfirm) {
58 | mPickerOptions.textContentConfirm = textContentConfirm;
59 | return this;
60 | }
61 |
62 | public TimePickerBuilder isDialog(boolean isDialog) {
63 | mPickerOptions.isDialog = isDialog;
64 | return this;
65 | }
66 |
67 | public TimePickerBuilder setCancelText(String textContentCancel) {
68 | mPickerOptions.textContentCancel = textContentCancel;
69 | return this;
70 | }
71 |
72 | public TimePickerBuilder setTitleText(String textContentTitle) {
73 | mPickerOptions.textContentTitle = textContentTitle;
74 | return this;
75 | }
76 |
77 | public TimePickerBuilder setSubmitColor(int textColorConfirm) {
78 | mPickerOptions.textColorConfirm = textColorConfirm;
79 | return this;
80 | }
81 |
82 | public TimePickerBuilder setCancelColor(int textColorCancel) {
83 | mPickerOptions.textColorCancel = textColorCancel;
84 | return this;
85 | }
86 |
87 | public TimePickerBuilder setHourList(List hourList) {
88 | mPickerOptions.hourList = hourList;
89 | return this;
90 | }
91 |
92 | public TimePickerBuilder setMinuteList(List minuteList) {
93 | mPickerOptions.minuteList = minuteList;
94 | return this;
95 | }
96 |
97 | public TimePickerBuilder setSecondList(List secondList) {
98 | mPickerOptions.secondList = secondList;
99 | return this;
100 | }
101 |
102 | /**
103 | * ViewGroup 类型的容器
104 | *
105 | * @param decorView 选择器会被添加到此容器中
106 | * @return TimePickerBuilder
107 | */
108 | public TimePickerBuilder setDecorView(ViewGroup decorView) {
109 | mPickerOptions.decorView = decorView;
110 | return this;
111 | }
112 |
113 | public TimePickerBuilder setBgColor(int bgColorWheel) {
114 | mPickerOptions.bgColorWheel = bgColorWheel;
115 | return this;
116 | }
117 |
118 | public TimePickerBuilder setTitleBgColor(int bgColorTitle) {
119 | mPickerOptions.bgColorTitle = bgColorTitle;
120 | return this;
121 | }
122 |
123 | public TimePickerBuilder setTitleColor(int textColorTitle) {
124 | mPickerOptions.textColorTitle = textColorTitle;
125 | return this;
126 | }
127 |
128 | public TimePickerBuilder setSubCalSize(int textSizeSubmitCancel) {
129 | mPickerOptions.textSizeSubmitCancel = textSizeSubmitCancel;
130 | return this;
131 | }
132 |
133 | public TimePickerBuilder setTitleSize(int textSizeTitle) {
134 | mPickerOptions.textSizeTitle = textSizeTitle;
135 | return this;
136 | }
137 |
138 | public TimePickerBuilder setContentTextSize(int textSizeContent) {
139 | mPickerOptions.textSizeContent = textSizeContent;
140 | return this;
141 | }
142 |
143 | /**
144 | * 设置最大可见数目
145 | *
146 | * @param count suggest value: 3, 5, 7, 9
147 | */
148 | public TimePickerBuilder setItemVisibleCount(int count) {
149 | mPickerOptions.itemsVisibleCount = count;
150 | return this;
151 | }
152 |
153 | /**
154 | * 透明度是否渐变
155 | *
156 | * @param isAlphaGradient true of false
157 | */
158 | public TimePickerBuilder isAlphaGradient(boolean isAlphaGradient) {
159 | mPickerOptions.isAlphaGradient = isAlphaGradient;
160 | return this;
161 | }
162 |
163 | /**
164 | * 因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
165 | *
166 | * @param date
167 | * @return TimePickerBuilder
168 | */
169 | public TimePickerBuilder setDate(Calendar date) {
170 | mPickerOptions.date = date;
171 | return this;
172 | }
173 |
174 | public TimePickerBuilder setLayoutRes(int res, CustomListener customListener) {
175 | mPickerOptions.layoutRes = res;
176 | mPickerOptions.customListener = customListener;
177 | return this;
178 | }
179 |
180 |
181 | /**
182 | * 设置起始时间
183 | * 因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
184 | */
185 |
186 | public TimePickerBuilder setRangDate(Calendar startDate, Calendar endDate) {
187 | mPickerOptions.startDate = startDate;
188 | mPickerOptions.endDate = endDate;
189 | return this;
190 | }
191 |
192 |
193 | /**
194 | * 设置间距倍数,但是只能在1.0-4.0f之间
195 | *
196 | * @param lineSpacingMultiplier
197 | */
198 | public TimePickerBuilder setLineSpacingMultiplier(float lineSpacingMultiplier) {
199 | mPickerOptions.lineSpacingMultiplier = lineSpacingMultiplier;
200 | return this;
201 | }
202 |
203 | /**
204 | * 设置分割线的颜色
205 | *
206 | * @param dividerColor
207 | */
208 |
209 | public TimePickerBuilder setDividerColor(@ColorInt int dividerColor) {
210 | mPickerOptions.dividerColor = dividerColor;
211 | return this;
212 | }
213 |
214 | /**
215 | * 设置分割线的类型
216 | *
217 | * @param dividerType
218 | */
219 | public TimePickerBuilder setDividerType(WheelView.DividerType dividerType) {
220 | mPickerOptions.dividerType = dividerType;
221 | return this;
222 | }
223 |
224 | /**
225 | * {@link #setOutSideColor} instead.
226 | *
227 | * @param backgroundId color resId.
228 | */
229 | @Deprecated
230 | public TimePickerBuilder setBackgroundId(int backgroundId) {
231 | mPickerOptions.outSideColor = backgroundId;
232 | return this;
233 | }
234 |
235 | /**
236 | * 显示时的外部背景色颜色,默认是灰色
237 | *
238 | * @param outSideColor
239 | */
240 | public TimePickerBuilder setOutSideColor(@ColorInt int outSideColor) {
241 | mPickerOptions.outSideColor = outSideColor;
242 | return this;
243 | }
244 |
245 | /**
246 | * 设置分割线之间的文字的颜色
247 | *
248 | * @param textColorCenter
249 | */
250 | public TimePickerBuilder setTextColorCenter(@ColorInt int textColorCenter) {
251 | mPickerOptions.textColorCenter = textColorCenter;
252 | return this;
253 | }
254 |
255 | /**
256 | * 设置分割线以外文字的颜色
257 | *
258 | * @param textColorOut
259 | */
260 | public TimePickerBuilder setTextColorOut(@ColorInt int textColorOut) {
261 | mPickerOptions.textColorOut = textColorOut;
262 | return this;
263 | }
264 |
265 | public TimePickerBuilder isCyclic(boolean cyclic) {
266 | mPickerOptions.cyclic = cyclic;
267 | return this;
268 | }
269 |
270 | public TimePickerBuilder setOutSideCancelable(boolean cancelable) {
271 | mPickerOptions.cancelable = cancelable;
272 | return this;
273 | }
274 |
275 | public TimePickerBuilder setLunarCalendar(boolean lunarCalendar) {
276 | mPickerOptions.isLunarCalendar = lunarCalendar;
277 | return this;
278 | }
279 |
280 |
281 | public TimePickerBuilder setLabel(String label_year, String label_month, String label_day, String label_hours, String label_mins, String label_seconds) {
282 | mPickerOptions.label_year = label_year;
283 | mPickerOptions.label_month = label_month;
284 | mPickerOptions.label_day = label_day;
285 | mPickerOptions.label_hours = label_hours;
286 | mPickerOptions.label_minutes = label_mins;
287 | mPickerOptions.label_seconds = label_seconds;
288 | return this;
289 | }
290 |
291 | /**
292 | * 设置X轴倾斜角度[ -90 , 90°]
293 | *
294 | * @param x_offset_year 年
295 | * @param x_offset_month 月
296 | * @param x_offset_day 日
297 | * @param x_offset_hours 时
298 | * @param x_offset_minutes 分
299 | * @param x_offset_seconds 秒
300 | * @return
301 | */
302 | public TimePickerBuilder setTextXOffset(int x_offset_year, int x_offset_month, int x_offset_day,
303 | int x_offset_hours, int x_offset_minutes, int x_offset_seconds) {
304 | mPickerOptions.x_offset_year = x_offset_year;
305 | mPickerOptions.x_offset_month = x_offset_month;
306 | mPickerOptions.x_offset_day = x_offset_day;
307 | mPickerOptions.x_offset_hours = x_offset_hours;
308 | mPickerOptions.x_offset_minutes = x_offset_minutes;
309 | mPickerOptions.x_offset_seconds = x_offset_seconds;
310 | return this;
311 | }
312 |
313 | public TimePickerBuilder isCenterLabel(boolean isCenterLabel) {
314 | mPickerOptions.isCenterLabel = isCenterLabel;
315 | return this;
316 | }
317 |
318 | /**
319 | * @param listener 切换item项滚动停止时,实时回调监听。
320 | * @return
321 | */
322 | public TimePickerBuilder setTimeSelectChangeListener(OnTimeSelectChangeListener listener) {
323 | mPickerOptions.timeSelectChangeListener = listener;
324 | return this;
325 | }
326 |
327 | public TimePickerView build() {
328 | return new TimePickerView(mPickerOptions);
329 | }
330 | }
331 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.configure;
2 |
3 | import android.content.Context;
4 | import android.graphics.Typeface;
5 | import android.view.Gravity;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 |
9 | import com.bigkoo.pickerview.R;
10 | import com.bigkoo.pickerview.listener.CustomListener;
11 | import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
12 | import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
13 | import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
14 | import com.bigkoo.pickerview.listener.OnTimeSelectListener;
15 | import com.contrarywind.view.WheelView;
16 |
17 | import java.util.Calendar;
18 | import java.util.List;
19 |
20 | /**
21 | * Build Options
22 | * Created by xiaosongzeem on 2018/3/8.
23 | */
24 |
25 | public class PickerOptions {
26 |
27 | //constant
28 | private static final int PICKER_VIEW_BTN_COLOR_NORMAL = 0xFF057dff;
29 | private static final int PICKER_VIEW_BG_COLOR_TITLE = 0xFFf5f5f5;
30 | private static final int PICKER_VIEW_COLOR_TITLE = 0xFF000000;
31 | private static final int PICKER_VIEW_BG_COLOR_DEFAULT = 0xFFFFFFFF;
32 |
33 | public static final int TYPE_PICKER_OPTIONS = 1;
34 | public static final int TYPE_PICKER_TIME = 2;
35 |
36 | public OnOptionsSelectListener optionsSelectListener;
37 | public OnTimeSelectListener timeSelectListener;
38 | public View.OnClickListener cancelListener;
39 |
40 | public OnTimeSelectChangeListener timeSelectChangeListener;
41 | public OnOptionsSelectChangeListener optionsSelectChangeListener;
42 | public CustomListener customListener;
43 |
44 | //options picker
45 | public String label1, label2, label3;//单位字符
46 | public int option1, option2, option3;//默认选中项
47 | public int x_offset_one, x_offset_two, x_offset_three;//x轴偏移量
48 |
49 | public boolean cyclic1 = false;//是否循环,默认否
50 | public boolean cyclic2 = false;
51 | public boolean cyclic3 = false;
52 |
53 | public boolean isRestoreItem = false; //切换时,还原第一项
54 |
55 |
56 | //time picker
57 | public boolean[] type = new boolean[]{true, true, true, false, false, false};//显示类型,默认显示: 年月日
58 |
59 | public Calendar date;//当前选中时间
60 | public Calendar startDate;//开始时间
61 | public Calendar endDate;//终止时间
62 | public int startYear;//开始年份
63 | public int endYear;//结尾年份
64 |
65 | public boolean cyclic = false;//是否循环
66 | public boolean isLunarCalendar = false;//是否显示农历
67 |
68 | public String label_year, label_month, label_day, label_hours, label_minutes, label_seconds;//单位
69 | public int x_offset_year, x_offset_month, x_offset_day, x_offset_hours, x_offset_minutes, x_offset_seconds;//单位
70 |
71 |
72 | public PickerOptions(int buildType) {
73 | if (buildType == TYPE_PICKER_OPTIONS) {
74 | layoutRes = R.layout.pickerview_options;
75 | } else {
76 | layoutRes = R.layout.pickerview_time;
77 | }
78 | }
79 |
80 | //******* general field ******//
81 | public int layoutRes;
82 | public ViewGroup decorView;
83 | public int textGravity = Gravity.CENTER;
84 | public Context context;
85 |
86 | public String textContentConfirm;//确定按钮文字
87 | public String textContentCancel;//取消按钮文字
88 | public String textContentTitle;//标题文字
89 |
90 | public int textColorConfirm = PICKER_VIEW_BTN_COLOR_NORMAL;//确定按钮颜色
91 | public int textColorCancel = PICKER_VIEW_BTN_COLOR_NORMAL;//取消按钮颜色
92 | public int textColorTitle = PICKER_VIEW_COLOR_TITLE;//标题颜色
93 |
94 | public int bgColorWheel = PICKER_VIEW_BG_COLOR_DEFAULT;//滚轮背景颜色
95 | public int bgColorTitle = PICKER_VIEW_BG_COLOR_TITLE;//标题背景颜色
96 |
97 | public int textSizeSubmitCancel = 17;//确定取消按钮大小
98 | public int textSizeTitle = 18;//标题文字大小
99 | public int textSizeContent = 18;//内容文字大小
100 |
101 | public int textColorOut = 0xFFa8a8a8; //分割线以外的文字颜色
102 | public int textColorCenter = 0xFF2a2a2a; //分割线之间的文字颜色
103 | public int dividerColor = 0xFFd5d5d5; //分割线的颜色
104 | public int outSideColor = -1; //显示时的外部背景色颜色,默认是灰色
105 |
106 | public float lineSpacingMultiplier = 1.6f; // 条目间距倍数 默认1.6
107 | public boolean isDialog;//是否是对话框模式
108 |
109 | public boolean cancelable = true;//是否能取消
110 | public boolean isCenterLabel = false;//是否只显示中间的label,默认每个item都显示
111 | public Typeface font = Typeface.MONOSPACE;//字体样式
112 | public WheelView.DividerType dividerType = WheelView.DividerType.FILL;//分隔线类型
113 | public int itemsVisibleCount = 9; //最大可见条目数
114 | public boolean isAlphaGradient = false; //透明度渐变
115 |
116 | // 自定义小时的数据,数值:0-23
117 | public List hourList;
118 | // 自定义分的数据,数值:0-59
119 | public List minuteList;
120 | // 自定义秒的数据,数值:0-59
121 | public List secondList;
122 | }
123 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/CustomListener.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by KyuYi on 2017/3/2.
7 | * E-Mail:kyu_yi@sina.com
8 | * 功能:
9 | */
10 |
11 | public interface CustomListener {
12 | void customLayout(View v);
13 | }
14 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/ISelectTimeCallback.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | /**
4 | * Created by zengsong on 2018/3/21.
5 | */
6 |
7 | public interface ISelectTimeCallback {
8 |
9 | public void onTimeSelectChanged();
10 | }
11 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/OnDismissListener.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | /**
4 | * Created by Sai on 15/8/9.
5 | */
6 | public interface OnDismissListener {
7 | public void onDismiss(Object o);
8 | }
9 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/OnOptionsSelectChangeListener.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | /**
4 | * Created by xiaosong on 2018/3/20.
5 | */
6 |
7 | public interface OnOptionsSelectChangeListener {
8 |
9 | void onOptionsSelectChanged(int options1, int options2, int options3);
10 |
11 | }
12 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/OnOptionsSelectListener.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by xiaosong on 2018/3/20.
7 | */
8 |
9 | public interface OnOptionsSelectListener {
10 |
11 | void onOptionsSelect(int options1, int options2, int options3, View v);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/OnTimeSelectChangeListener.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | import java.util.Date;
4 |
5 | /**
6 | * Created by xiaosong on 2018/3/20.
7 | */
8 |
9 | public interface OnTimeSelectChangeListener {
10 |
11 | void onTimeSelectChanged(Date date);
12 | }
13 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/listener/OnTimeSelectListener.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.listener;
2 |
3 | import android.view.View;
4 |
5 | import java.util.Date;
6 |
7 | /**
8 | * Created by xiaosong on 2018/3/20.
9 | */
10 |
11 | public interface OnTimeSelectListener {
12 |
13 | void onTimeSelect(Date date, View v);
14 | }
15 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/utils/ChinaDate.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.utils;
2 |
3 | import java.text.SimpleDateFormat;
4 | import java.util.ArrayList;
5 | import java.util.Calendar;
6 | import java.util.Date;
7 | import java.util.GregorianCalendar;
8 | import java.util.Locale;
9 |
10 | public class ChinaDate {
11 |
12 | /**
13 | *
14 | *
15 | * 0x代表十六进制,后面的五位数是十六进制数。
16 | * 举个例子: 1980年的数据是 0x095b0
17 | * 二进制: 0000 1001 0101 1011 0000
18 | * 1-4: 表示当年是否为闰年,是的话为1,否则为0。
19 | * 5-16: 为除了闰月外的正常月份是大月还是小月,1为30天,0为29天。
20 | * 注意: 从1月到12月对应的是第16位到第5位。
21 | * 17-20: 非闰年为0,大于0表示闰月月份,仅当存在闰月的情况下有意义。
22 | */
23 | final private static long[] lunarInfo = new long[]{
24 | 0x04bd8, 0x04ae0, 0x0a570, 0x054d5, 0x0d260, 0x0d950, 0x16554, 0x056a0, 0x09ad0, 0x055d2,//1900-1909
25 | 0x04ae0, 0x0a5b6, 0x0a4d0, 0x0d250, 0x1d255, 0x0b540, 0x0d6a0, 0x0ada2, 0x095b0, 0x14977,//1910-1919
26 | 0x04970, 0x0a4b0, 0x0b4b5, 0x06a50, 0x06d40, 0x1ab54, 0x02b60, 0x09570, 0x052f2, 0x04970,//1920-1929
27 | 0x06566, 0x0d4a0, 0x0ea50, 0x06e95, 0x05ad0, 0x02b60, 0x186e3, 0x092e0, 0x1c8d7, 0x0c950,//1930-1939
28 | 0x0d4a0, 0x1d8a6, 0x0b550, 0x056a0, 0x1a5b4, 0x025d0, 0x092d0, 0x0d2b2, 0x0a950, 0x0b557,//1940-1949
29 | 0x06ca0, 0x0b550, 0x15355, 0x04da0, 0x0a5b0, 0x14573, 0x052b0, 0x0a9a8, 0x0e950, 0x06aa0,//1950-1959
30 | 0x0aea6, 0x0ab50, 0x04b60, 0x0aae4, 0x0a570, 0x05260, 0x0f263, 0x0d950, 0x05b57, 0x056a0,//1960-1969
31 | 0x096d0, 0x04dd5, 0x04ad0, 0x0a4d0, 0x0d4d4, 0x0d250, 0x0d558, 0x0b540, 0x0b6a0, 0x195a6,//1970-1979
32 | 0x095b0, 0x049b0, 0x0a974, 0x0a4b0, 0x0b27a, 0x06a50, 0x06d40, 0x0af46, 0x0ab60, 0x09570,//1980-1989
33 | 0x04af5, 0x04970, 0x064b0, 0x074a3, 0x0ea50, 0x06b58, 0x055c0, 0x0ab60, 0x096d5, 0x092e0,//1990-1999
34 | 0x0c960, 0x0d954, 0x0d4a0, 0x0da50, 0x07552, 0x056a0, 0x0abb7, 0x025d0, 0x092d0, 0x0cab5,//2000-2009
35 | 0x0a950, 0x0b4a0, 0x0baa4, 0x0ad50, 0x055d9, 0x04ba0, 0x0a5b0, 0x15176, 0x052b0, 0x0a930,//2010-2019
36 | 0x07954, 0x06aa0, 0x0ad50, 0x05b52, 0x04b60, 0x0a6e6, 0x0a4e0, 0x0d260, 0x0ea65, 0x0d530,//2020-2029
37 | 0x05aa0, 0x076a3, 0x096d0, 0x04afb, 0x04ad0, 0x0a4d0, 0x1d0b6, 0x0d250, 0x0d520, 0x0dd45,//2030-2039
38 | 0x0b5a0, 0x056d0, 0x055b2, 0x049b0, 0x0a577, 0x0a4b0, 0x0aa50, 0x1b255, 0x06d20, 0x0ada0,//2040-2049
39 | 0x14b63, 0x09370, 0x049f8, 0x04970, 0x064b0, 0x168a6, 0x0ea50, 0x06b20, 0x1a6c4, 0x0aae0,//2050-2059
40 | 0x0a2e0, 0x0d2e3, 0x0c960, 0x0d557, 0x0d4a0, 0x0da50, 0x05d55, 0x056a0, 0x0a6d0, 0x055d4,//2060-2069
41 | 0x052d0, 0x0a9b8, 0x0a950, 0x0b4a0, 0x0b6a6, 0x0ad50, 0x055a0, 0x0aba4, 0x0a5b0, 0x052b0,//2070-2079
42 | 0x0b273, 0x06930, 0x07337, 0x06aa0, 0x0ad50, 0x14b55, 0x04b60, 0x0a570, 0x054e4, 0x0d160,//2080-2089
43 | 0x0e968, 0x0d520, 0x0daa0, 0x16aa6, 0x056d0, 0x04ae0, 0x0a9d4, 0x0a2d0, 0x0d150, 0x0f252,//2090-2099
44 | 0x0d520};//2100
45 |
46 | private final static String[] nStr1 = new String[]{"", "正", "二", "三", "四",
47 | "五", "六", "七", "八", "九", "十", "冬", "腊"};
48 | private final static String[] Gan = new String[]{"甲", "乙", "丙", "丁", "戊",
49 | "己", "庚", "辛", "壬", "癸"};
50 | private final static String[] Zhi = new String[]{"子", "丑", "寅", "卯", "辰",
51 | "巳", "午", "未", "申", "酉", "戌", "亥"};
52 | private final static String[] Animals = new String[]{"鼠", "牛", "虎", "兔",
53 | "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};
54 |
55 | /**
56 | * 传回农历
57 | *
58 | * @param y 年的总天数
59 | * @return 农历
60 | */
61 | final private static int lYearDays(int y) {
62 | int i, sum = 348;
63 | for (i = 0x8000; i > 0x8; i >>= 1) {
64 | if ((lunarInfo[y - 1900] & i) != 0)
65 | sum += 1;
66 | }
67 | return (sum + leapDays(y));
68 | }
69 |
70 | /**
71 | * 传回农历
72 | *
73 | * @param y 年闰月的天数
74 | * @return 农历
75 | */
76 | final public static int leapDays(int y) {
77 | if (leapMonth(y) != 0) {
78 | if ((lunarInfo[y - 1900] & 0x10000) != 0)
79 | return 30;
80 | else
81 | return 29;
82 | } else
83 | return 0;
84 | }
85 |
86 | /**
87 | * 传回农历
88 | *
89 | * @param y 年闰哪个月 1-12 , 没闰传回 0
90 | * @return 农历
91 | */
92 | final public static int leapMonth(int y) {
93 | return (int) (lunarInfo[y - 1900] & 0xf);
94 | }
95 |
96 | /**
97 | * 传回农历 y
98 | *
99 | * @param y y年m月的总天数
100 | * @param m y年m月的总天数
101 | * @return 农历
102 | */
103 | final public static int monthDays(int y, int m) {
104 | if ((lunarInfo[y - 1900] & (0x10000 >> m)) == 0)
105 | return 29;
106 | else
107 | return 30;
108 | }
109 |
110 | /**
111 | * 传回农历
112 | *
113 | * @param y 年的生肖
114 | * @return
115 | */
116 | final public static String AnimalsYear(int y) {
117 | return Animals[(y - 4) % 12];
118 | }
119 |
120 | /**
121 | * 传入
122 | *
123 | * @param num 月日的offset 传回干支,0是甲子
124 | * @return 干支
125 | */
126 | final private static String cyclicalm(int num) {
127 | return (Gan[num % 10] + Zhi[num % 12]);
128 | }
129 |
130 | /**
131 | * 传入 offset 传回干支
132 | *
133 | * @param y 0是甲子
134 | * @return 干支
135 | */
136 | final public static String cyclical(int y) {
137 | int num = y - 1900 + 36;
138 | return (cyclicalm(num));
139 | }
140 |
141 |
142 | /**
143 | * 传出y年m月d日对应的农历.year0 .month1 .day2 .yearCyl3 .monCyl4 .dayCyl5 .isLeap6
144 | *
145 | * @param y 年
146 | * @param m 月
147 | * @param d 日
148 | * @return y年m月d日对应的农历
149 | */
150 | final public static long[] calElement(int y, int m, int d) {
151 | long[] nongDate = new long[7];
152 | int i = 0, temp = 0, leap = 0;
153 | Date baseDate = new GregorianCalendar(0 + 1900, 0, 31).getTime();
154 | Date objDate = new GregorianCalendar(y, m - 1, d).getTime();
155 | long offset = (objDate.getTime() - baseDate.getTime()) / 86400000L;
156 | nongDate[5] = offset + 40;
157 | nongDate[4] = 14;
158 | for (i = 1900; i < 2100 && offset > 0; i++) {
159 | temp = lYearDays(i);
160 | offset -= temp;
161 | nongDate[4] += 12;
162 | }
163 | if (offset < 0) {
164 | offset += temp;
165 | i--;
166 | nongDate[4] -= 12;
167 | }
168 | nongDate[0] = i;
169 | nongDate[3] = i - 1864;
170 | leap = leapMonth(i); // 闰哪个月
171 | nongDate[6] = 0;
172 | for (i = 1; i < 13 && offset > 0; i++) {
173 | // 闰月
174 | if (leap > 0 && i == (leap + 1) && nongDate[6] == 0) {
175 | --i;
176 | nongDate[6] = 1;
177 | temp = leapDays((int) nongDate[0]);
178 | } else {
179 | temp = monthDays((int) nongDate[0], i);
180 | }
181 | // 解除闰月
182 | if (nongDate[6] == 1 && i == (leap + 1))
183 | nongDate[6] = 0;
184 | offset -= temp;
185 | if (nongDate[6] == 0)
186 | nongDate[4]++;
187 | }
188 | if (offset == 0 && leap > 0 && i == leap + 1) {
189 | if (nongDate[6] == 1) {
190 | nongDate[6] = 0;
191 | } else {
192 | nongDate[6] = 1;
193 | --i;
194 | --nongDate[4];
195 | }
196 | }
197 | if (offset < 0) {
198 | offset += temp;
199 | --i;
200 | --nongDate[4];
201 | }
202 | nongDate[1] = i;
203 | nongDate[2] = offset + 1;
204 | return nongDate;
205 | }
206 |
207 | public final static String getChinaDate(int day) {
208 | String a = "";
209 | if (day == 10)
210 | return "初十";
211 | if (day == 20)
212 | return "二十";
213 | if (day == 30)
214 | return "三十";
215 | int two = (int) ((day) / 10);
216 | if (two == 0)
217 | a = "初";
218 | if (two == 1)
219 | a = "十";
220 | if (two == 2)
221 | a = "廿";
222 | if (two == 3)
223 | a = "三";
224 | int one = (int) (day % 10);
225 | switch (one) {
226 | case 1:
227 | a += "一";
228 | break;
229 | case 2:
230 | a += "二";
231 | break;
232 | case 3:
233 | a += "三";
234 | break;
235 | case 4:
236 | a += "四";
237 | break;
238 | case 5:
239 | a += "五";
240 | break;
241 | case 6:
242 | a += "六";
243 | break;
244 | case 7:
245 | a += "七";
246 | break;
247 | case 8:
248 | a += "八";
249 | break;
250 | case 9:
251 | a += "九";
252 | break;
253 | }
254 | return a;
255 | }
256 |
257 | public static String getCurrentLunarDate() {
258 | Calendar today = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
259 | int year = today.get(Calendar.YEAR);
260 | int month = today.get(Calendar.MONTH) + 1;
261 | int date = today.get(Calendar.DATE);
262 | long[] l = calElement(year, month, date);
263 | StringBuffer sToday = new StringBuffer();
264 | try {
265 | sToday.append(sdf.format(today.getTime()));
266 | sToday.append(" 农历");
267 | sToday.append(cyclical(year));
268 | sToday.append('(');
269 | sToday.append(AnimalsYear(year));
270 | sToday.append(")年");
271 | sToday.append(nStr1[(int) l[1]]);
272 | sToday.append("月");
273 | sToday.append(getChinaDate((int) (l[2])));
274 | return sToday.toString();
275 | } finally {
276 | sToday = null;
277 | }
278 | }
279 |
280 | public static String oneDay(int year, int month, int day) {
281 | // Calendar today = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
282 | long[] l = calElement(year, month, day);
283 | StringBuffer sToday = new StringBuffer();
284 | try {
285 | // sToday.append(sdf.format(today.getTime()));
286 | sToday.append(" 农历");
287 | sToday.append(cyclical(year));
288 | sToday.append('(');
289 | sToday.append(AnimalsYear(year));
290 | sToday.append(")年");
291 | sToday.append(nStr1[(int) l[1]]);
292 | sToday.append("月");
293 | sToday.append(getChinaDate((int) (l[2])));
294 | return sToday.toString();
295 | } finally {
296 | sToday = null;
297 | }
298 | }
299 |
300 | private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年M月d日 EEEEE");
301 |
302 |
303 | /**
304 | * @param lunarYear 农历年份
305 | * @return String of Ganzhi: 甲子年
306 | * 甲乙丙丁戊己庚辛壬癸
307 | * 子丑寅卯辰巳无为申酉戌亥
308 | */
309 | public static String getLunarYearText(int lunarYear) {
310 | return Gan[(lunarYear - 4) % 10] + Zhi[(lunarYear - 4) % 12] + "年";
311 | }
312 |
313 |
314 | public static ArrayList getYears(int startYear, int endYear) {
315 | ArrayList years = new ArrayList<>();
316 | for (int i = startYear; i < endYear; i++) {
317 | years.add(String.format("%s(%d)", getLunarYearText(i), i));
318 | }
319 | return years;
320 | }
321 |
322 | /**
323 | * 获取year年的所有月份
324 | *
325 | * @param year 年
326 | * @return 月份列表
327 | */
328 | public static ArrayList getMonths(int year) {
329 | ArrayList baseMonths = new ArrayList<>();
330 | for (int i = 1; i < nStr1.length; i++) {
331 | baseMonths.add(nStr1[i] + "月");
332 | }
333 | if (leapMonth(year) != 0) {
334 | baseMonths.add(leapMonth(year), "闰" + nStr1[leapMonth(year)] + "月");
335 | }
336 | return baseMonths;
337 | }
338 |
339 | /**
340 | * 获取每月农历显示名称
341 | *
342 | * @param maxDay 天
343 | * @return 名称列表
344 | */
345 | public static ArrayList getLunarDays(int maxDay) {
346 | ArrayList days = new ArrayList<>();
347 | for (int i = 1; i <= maxDay; i++) {
348 | days.add(getChinaDate(i));
349 | }
350 | return days;
351 | }
352 | }
353 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/utils/PickerViewAnimateUtil.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.utils;
2 |
3 | import android.view.Gravity;
4 |
5 | import com.bigkoo.pickerview.R;
6 |
7 | /**
8 | * Created by Sai on 15/8/9.
9 | */
10 | public class PickerViewAnimateUtil {
11 | private static final int INVALID = -1;
12 | /**
13 | * Get default animation resource when not defined by the user
14 | *
15 | * @param gravity the animGravity of the dialog
16 | * @param isInAnimation determine if is in or out animation. true when is is
17 | * @return the id of the animation resource
18 | */
19 | public static int getAnimationResource(int gravity, boolean isInAnimation) {
20 | switch (gravity) {
21 | case Gravity.BOTTOM:
22 | return isInAnimation ? R.anim.pickerview_slide_in_bottom : R.anim.pickerview_slide_out_bottom;
23 | }
24 | return INVALID;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/view/BasePickerView.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.view;
2 |
3 | import android.app.Activity;
4 | import android.app.Dialog;
5 | import android.content.Context;
6 | import android.content.DialogInterface;
7 | import android.graphics.Color;
8 | import android.view.Gravity;
9 | import android.view.KeyEvent;
10 | import android.view.LayoutInflater;
11 | import android.view.MotionEvent;
12 | import android.view.View;
13 | import android.view.ViewGroup;
14 | import android.view.Window;
15 | import android.view.animation.Animation;
16 | import android.view.animation.AnimationUtils;
17 | import android.widget.FrameLayout;
18 |
19 | import com.bigkoo.pickerview.R;
20 | import com.bigkoo.pickerview.configure.PickerOptions;
21 | import com.bigkoo.pickerview.listener.OnDismissListener;
22 | import com.bigkoo.pickerview.utils.PickerViewAnimateUtil;
23 |
24 | /**
25 | * Created by Sai on 15/11/22.
26 | * 精仿iOSPickerViewController控件
27 | */
28 | public class BasePickerView {
29 |
30 | private Context context;
31 | protected ViewGroup contentContainer;
32 | private ViewGroup rootView;//附加View 的 根View
33 | private ViewGroup dialogView;//附加Dialog 的 根View
34 |
35 | protected PickerOptions mPickerOptions;
36 | private OnDismissListener onDismissListener;
37 | private boolean dismissing;
38 |
39 | private Animation outAnim;
40 | private Animation inAnim;
41 | private boolean isShowing;
42 |
43 | protected int animGravity = Gravity.BOTTOM;
44 |
45 | private Dialog mDialog;
46 | protected View clickView;//是通过哪个View弹出的
47 | private boolean isAnim = true;
48 |
49 | public BasePickerView(Context context) {
50 | this.context = context;
51 | }
52 |
53 |
54 | protected void initViews() {
55 |
56 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
57 | ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
58 |
59 | LayoutInflater layoutInflater = LayoutInflater.from(context);
60 | if (isDialog()) {
61 | //如果是对话框模式
62 | dialogView = (ViewGroup) layoutInflater.inflate(R.layout.layout_basepickerview, null, false);
63 | //设置界面的背景为透明
64 | dialogView.setBackgroundColor(Color.TRANSPARENT);
65 | //这个是真正要加载选择器的父布局
66 | contentContainer = (ViewGroup) dialogView.findViewById(R.id.content_container);
67 | //设置对话框 默认左右间距屏幕30
68 | params.leftMargin = 30;
69 | params.rightMargin = 30;
70 | contentContainer.setLayoutParams(params);
71 | //创建对话框
72 | createDialog();
73 | //给背景设置点击事件,这样当点击内容以外的地方会关闭界面
74 | dialogView.setOnClickListener(new View.OnClickListener() {
75 | @Override
76 | public void onClick(View view) {
77 | dismiss();
78 | }
79 | });
80 | } else {
81 | //如果只是要显示在屏幕的下方
82 | //decorView是activity的根View,包含 contentView 和 titleView
83 | if (mPickerOptions.decorView == null) {
84 | mPickerOptions.decorView = (ViewGroup) ((Activity) context).getWindow().getDecorView();
85 | }
86 | //将控件添加到decorView中
87 | rootView = (ViewGroup) layoutInflater.inflate(R.layout.layout_basepickerview, mPickerOptions.decorView, false);
88 | rootView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
89 | if (mPickerOptions.outSideColor != -1) {
90 | rootView.setBackgroundColor(mPickerOptions.outSideColor);
91 | }
92 | //这个是真正要加载时间选取器的父布局
93 | contentContainer = (ViewGroup) rootView.findViewById(R.id.content_container);
94 | contentContainer.setLayoutParams(params);
95 | }
96 | setKeyBackCancelable(true);
97 | }
98 |
99 | protected void initAnim() {
100 | inAnim = getInAnimation();
101 | outAnim = getOutAnimation();
102 | }
103 |
104 | protected void initEvents() {
105 | }
106 |
107 |
108 | /**
109 | * @param v (是通过哪个View弹出的)
110 | * @param isAnim 是否显示动画效果
111 | */
112 | public void show(View v, boolean isAnim) {
113 | this.clickView = v;
114 | this.isAnim = isAnim;
115 | show();
116 | }
117 |
118 | public void show(boolean isAnim) {
119 | show(null, isAnim);
120 | }
121 |
122 | public void show(View v) {
123 | this.clickView = v;
124 | show();
125 | }
126 |
127 |
128 | /**
129 | * 添加View到根视图
130 | */
131 | public void show() {
132 | if (isDialog()) {
133 | showDialog();
134 | } else {
135 | if (isShowing()) {
136 | return;
137 | }
138 | isShowing = true;
139 | onAttached(rootView);
140 | rootView.requestFocus();
141 | }
142 | }
143 |
144 |
145 | /**
146 | * show的时候调用
147 | *
148 | * @param view 这个View
149 | */
150 | private void onAttached(View view) {
151 | mPickerOptions.decorView.addView(view);
152 | if (isAnim) {
153 | contentContainer.startAnimation(inAnim);
154 | }
155 | }
156 |
157 |
158 | /**
159 | * 检测该View是不是已经添加到根视图
160 | *
161 | * @return 如果视图已经存在该View返回true
162 | */
163 | public boolean isShowing() {
164 | if (isDialog()) {
165 | return false;
166 | } else {
167 | return rootView.getParent() != null || isShowing;
168 | }
169 |
170 | }
171 |
172 | public void dismiss() {
173 | if (isDialog()) {
174 | dismissDialog();
175 | } else {
176 | if (dismissing) {
177 | return;
178 | }
179 |
180 | if (isAnim) {
181 | //消失动画
182 | outAnim.setAnimationListener(new Animation.AnimationListener() {
183 | @Override
184 | public void onAnimationStart(Animation animation) {
185 |
186 | }
187 |
188 | @Override
189 | public void onAnimationEnd(Animation animation) {
190 | dismissImmediately();
191 | }
192 |
193 | @Override
194 | public void onAnimationRepeat(Animation animation) {
195 |
196 | }
197 | });
198 | contentContainer.startAnimation(outAnim);
199 | } else {
200 | dismissImmediately();
201 | }
202 | dismissing = true;
203 | }
204 |
205 |
206 | }
207 |
208 | public void dismissImmediately() {
209 |
210 | mPickerOptions.decorView.post(new Runnable() {
211 | @Override
212 | public void run() {
213 | //从根视图移除
214 | mPickerOptions.decorView.removeView(rootView);
215 | isShowing = false;
216 | dismissing = false;
217 | if (onDismissListener != null) {
218 | onDismissListener.onDismiss(BasePickerView.this);
219 | }
220 | }
221 | });
222 |
223 |
224 | }
225 |
226 | private Animation getInAnimation() {
227 | int res = PickerViewAnimateUtil.getAnimationResource(this.animGravity, true);
228 | return AnimationUtils.loadAnimation(context, res);
229 | }
230 |
231 | private Animation getOutAnimation() {
232 | int res = PickerViewAnimateUtil.getAnimationResource(this.animGravity, false);
233 | return AnimationUtils.loadAnimation(context, res);
234 | }
235 |
236 | public BasePickerView setOnDismissListener(OnDismissListener onDismissListener) {
237 | this.onDismissListener = onDismissListener;
238 | return this;
239 | }
240 |
241 | public void setKeyBackCancelable(boolean isCancelable) {
242 |
243 | ViewGroup View;
244 | if (isDialog()) {
245 | View = dialogView;
246 | } else {
247 | View = rootView;
248 | }
249 |
250 | View.setFocusable(isCancelable);
251 | View.setFocusableInTouchMode(isCancelable);
252 | if (isCancelable) {
253 | View.setOnKeyListener(onKeyBackListener);
254 | } else {
255 | View.setOnKeyListener(null);
256 | }
257 | }
258 |
259 | private View.OnKeyListener onKeyBackListener = new View.OnKeyListener() {
260 | @Override
261 | public boolean onKey(View v, int keyCode, KeyEvent event) {
262 | if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_DOWN && isShowing()) {
263 | dismiss();
264 | return true;
265 | }
266 | return false;
267 | }
268 | };
269 |
270 | protected BasePickerView setOutSideCancelable(boolean isCancelable) {
271 |
272 | if (rootView != null) {
273 | View view = rootView.findViewById(R.id.outmost_container);
274 |
275 | if (isCancelable) {
276 | view.setOnTouchListener(onCancelableTouchListener);
277 | } else {
278 | view.setOnTouchListener(null);
279 | }
280 | }
281 |
282 | return this;
283 | }
284 |
285 | /**
286 | * 设置对话框模式是否可以点击外部取消
287 | */
288 | public void setDialogOutSideCancelable() {
289 | if (mDialog != null) {
290 | mDialog.setCancelable(mPickerOptions.cancelable);
291 | }
292 | }
293 |
294 |
295 | /**
296 | * Called when the user touch on black overlay, in order to dismiss the dialog.
297 | */
298 | private final View.OnTouchListener onCancelableTouchListener = new View.OnTouchListener() {
299 | @Override
300 | public boolean onTouch(View v, MotionEvent event) {
301 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
302 | dismiss();
303 | }
304 | return false;
305 | }
306 | };
307 |
308 | public View findViewById(int id) {
309 | return contentContainer.findViewById(id);
310 | }
311 |
312 | public void createDialog() {
313 | if (dialogView != null) {
314 | mDialog = new Dialog(context, R.style.custom_dialog2);
315 | mDialog.setCancelable(mPickerOptions.cancelable);//不能点外面取消,也不能点back取消
316 | mDialog.setContentView(dialogView);
317 |
318 | Window dialogWindow = mDialog.getWindow();
319 | if (dialogWindow != null) {
320 | dialogWindow.setWindowAnimations(R.style.picker_view_scale_anim);
321 | dialogWindow.setGravity(Gravity.CENTER);//可以改成Bottom
322 | }
323 |
324 | mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
325 | @Override
326 | public void onDismiss(DialogInterface dialog) {
327 | if (onDismissListener != null) {
328 | onDismissListener.onDismiss(BasePickerView.this);
329 | }
330 | }
331 | });
332 | }
333 | }
334 |
335 | private void showDialog() {
336 | if (mDialog != null) {
337 | mDialog.show();
338 | }
339 | }
340 |
341 | private void dismissDialog() {
342 | if (mDialog != null) {
343 | mDialog.dismiss();
344 | }
345 | }
346 |
347 | public ViewGroup getDialogContainerLayout() {
348 | return contentContainer;
349 | }
350 |
351 |
352 | public Dialog getDialog() {
353 | return mDialog;
354 | }
355 |
356 |
357 | public boolean isDialog() {
358 | return false;
359 | }
360 |
361 | }
362 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/view/OptionsPickerView.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.view;
2 |
3 | import android.content.Context;
4 | import android.text.TextUtils;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.widget.Button;
8 | import android.widget.LinearLayout;
9 | import android.widget.RelativeLayout;
10 | import android.widget.TextView;
11 |
12 | import com.bigkoo.pickerview.R;
13 | import com.bigkoo.pickerview.configure.PickerOptions;
14 |
15 | import java.util.List;
16 |
17 | /**
18 | * 条件选择器
19 | * Created by Sai on 15/11/22.
20 | */
21 | public class OptionsPickerView extends BasePickerView implements View.OnClickListener {
22 |
23 | private WheelOptions wheelOptions;
24 |
25 | private static final String TAG_SUBMIT = "submit";
26 | private static final String TAG_CANCEL = "cancel";
27 |
28 |
29 | public OptionsPickerView(PickerOptions pickerOptions) {
30 | super(pickerOptions.context);
31 | mPickerOptions = pickerOptions;
32 | initView(pickerOptions.context);
33 | }
34 |
35 | private void initView(Context context) {
36 | setDialogOutSideCancelable();
37 | initViews();
38 | initAnim();
39 | initEvents();
40 | if (mPickerOptions.customListener == null) {
41 | LayoutInflater.from(context).inflate(mPickerOptions.layoutRes, contentContainer);
42 |
43 | //顶部标题
44 | TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
45 | RelativeLayout rv_top_bar = (RelativeLayout) findViewById(R.id.rv_topbar);
46 |
47 | //确定和取消按钮
48 | Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
49 | Button btnCancel = (Button) findViewById(R.id.btnCancel);
50 |
51 | btnSubmit.setTag(TAG_SUBMIT);
52 | btnCancel.setTag(TAG_CANCEL);
53 | btnSubmit.setOnClickListener(this);
54 | btnCancel.setOnClickListener(this);
55 |
56 | //设置文字
57 | btnSubmit.setText(TextUtils.isEmpty(mPickerOptions.textContentConfirm) ? context.getResources().getString(R.string.pickerview_submit) : mPickerOptions.textContentConfirm);
58 | btnCancel.setText(TextUtils.isEmpty(mPickerOptions.textContentCancel) ? context.getResources().getString(R.string.pickerview_cancel) : mPickerOptions.textContentCancel);
59 | tvTitle.setText(TextUtils.isEmpty(mPickerOptions.textContentTitle) ? "" : mPickerOptions.textContentTitle);//默认为空
60 |
61 | //设置color
62 | btnSubmit.setTextColor(mPickerOptions.textColorConfirm);
63 | btnCancel.setTextColor(mPickerOptions.textColorCancel);
64 | tvTitle.setTextColor(mPickerOptions.textColorTitle);
65 | rv_top_bar.setBackgroundColor(mPickerOptions.bgColorTitle);
66 |
67 | //设置文字大小
68 | btnSubmit.setTextSize(mPickerOptions.textSizeSubmitCancel);
69 | btnCancel.setTextSize(mPickerOptions.textSizeSubmitCancel);
70 | tvTitle.setTextSize(mPickerOptions.textSizeTitle);
71 | } else {
72 | mPickerOptions.customListener.customLayout(LayoutInflater.from(context).inflate(mPickerOptions.layoutRes, contentContainer));
73 | }
74 |
75 | // ----滚轮布局
76 | final LinearLayout optionsPicker = (LinearLayout) findViewById(R.id.optionspicker);
77 | optionsPicker.setBackgroundColor(mPickerOptions.bgColorWheel);
78 |
79 | wheelOptions = new WheelOptions<>(optionsPicker, mPickerOptions.isRestoreItem);
80 | if (mPickerOptions.optionsSelectChangeListener != null) {
81 | wheelOptions.setOptionsSelectChangeListener(mPickerOptions.optionsSelectChangeListener);
82 | }
83 |
84 | wheelOptions.setTextContentSize(mPickerOptions.textSizeContent);
85 | wheelOptions.setItemsVisible(mPickerOptions.itemsVisibleCount);
86 | wheelOptions.setAlphaGradient(mPickerOptions.isAlphaGradient);
87 | wheelOptions.setLabels(mPickerOptions.label1, mPickerOptions.label2, mPickerOptions.label3);
88 | wheelOptions.setTextXOffset(mPickerOptions.x_offset_one, mPickerOptions.x_offset_two, mPickerOptions.x_offset_three);
89 | wheelOptions.setCyclic(mPickerOptions.cyclic1, mPickerOptions.cyclic2, mPickerOptions.cyclic3);
90 | wheelOptions.setTypeface(mPickerOptions.font);
91 |
92 | setOutSideCancelable(mPickerOptions.cancelable);
93 |
94 | wheelOptions.setDividerColor(mPickerOptions.dividerColor);
95 | wheelOptions.setDividerType(mPickerOptions.dividerType);
96 | wheelOptions.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
97 | wheelOptions.setTextColorOut(mPickerOptions.textColorOut);
98 | wheelOptions.setTextColorCenter(mPickerOptions.textColorCenter);
99 | wheelOptions.isCenterLabel(mPickerOptions.isCenterLabel);
100 | }
101 |
102 | /**
103 | * 动态设置标题
104 | *
105 | * @param text 标题文本内容
106 | */
107 | public void setTitleText(String text) {
108 | TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
109 | if (tvTitle != null) {
110 | tvTitle.setText(text);
111 | }
112 | }
113 |
114 | /**
115 | * 设置默认选中项
116 | *
117 | * @param option1
118 | */
119 | public void setSelectOptions(int option1) {
120 | mPickerOptions.option1 = option1;
121 | reSetCurrentItems();
122 | }
123 |
124 |
125 | public void setSelectOptions(int option1, int option2) {
126 | mPickerOptions.option1 = option1;
127 | mPickerOptions.option2 = option2;
128 | reSetCurrentItems();
129 | }
130 |
131 | public void setSelectOptions(int option1, int option2, int option3) {
132 | mPickerOptions.option1 = option1;
133 | mPickerOptions.option2 = option2;
134 | mPickerOptions.option3 = option3;
135 | reSetCurrentItems();
136 | }
137 |
138 | private void reSetCurrentItems() {
139 | if (wheelOptions != null) {
140 | wheelOptions.setCurrentItems(mPickerOptions.option1, mPickerOptions.option2, mPickerOptions.option3);
141 | }
142 | }
143 |
144 | public void setPicker(List optionsItems) {
145 | this.setPicker(optionsItems, null, null);
146 | }
147 |
148 | public void setPicker(List options1Items, List> options2Items) {
149 | this.setPicker(options1Items, options2Items, null);
150 | }
151 |
152 | public void setPicker(List options1Items,
153 | List> options2Items,
154 | List>> options3Items) {
155 |
156 | wheelOptions.setPicker(options1Items, options2Items, options3Items);
157 | reSetCurrentItems();
158 | }
159 |
160 |
161 | //不联动情况下调用
162 | public void setNPicker(List options1Items,
163 | List options2Items,
164 | List options3Items) {
165 |
166 | wheelOptions.setLinkage(false);
167 | wheelOptions.setNPicker(options1Items, options2Items, options3Items);
168 | reSetCurrentItems();
169 | }
170 |
171 | @Override
172 | public void onClick(View v) {
173 | String tag = (String) v.getTag();
174 | if (tag.equals(TAG_SUBMIT)) {
175 | returnData();
176 | } else if (tag.equals(TAG_CANCEL)) {
177 | if (mPickerOptions.cancelListener != null) {
178 | mPickerOptions.cancelListener.onClick(v);
179 | }
180 | }
181 | dismiss();
182 | }
183 |
184 | //抽离接口回调的方法
185 | public void returnData() {
186 | if (mPickerOptions.optionsSelectListener != null) {
187 | int[] optionsCurrentItems = wheelOptions.getCurrentItems();
188 | mPickerOptions.optionsSelectListener.onOptionsSelect(optionsCurrentItems[0], optionsCurrentItems[1], optionsCurrentItems[2], clickView);
189 | }
190 | }
191 |
192 |
193 | @Override
194 | public boolean isDialog() {
195 | return mPickerOptions.isDialog;
196 | }
197 | }
198 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.view;
2 |
3 | import android.content.Context;
4 | import android.text.TextUtils;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.widget.Button;
8 | import android.widget.LinearLayout;
9 | import android.widget.RelativeLayout;
10 | import android.widget.TextView;
11 |
12 | import com.bigkoo.pickerview.R;
13 | import com.bigkoo.pickerview.configure.PickerOptions;
14 | import com.bigkoo.pickerview.listener.ISelectTimeCallback;
15 |
16 | import java.text.ParseException;
17 | import java.util.Calendar;
18 | import java.util.Date;
19 |
20 | /**
21 | * 时间选择器
22 | * Created by Sai on 15/11/22.
23 | * Updated by XiaoSong on 2017-2-22.
24 | */
25 | public class TimePickerView extends BasePickerView implements View.OnClickListener {
26 |
27 | private WheelTime wheelTime; //自定义控件
28 | private static final String TAG_SUBMIT = "submit";
29 | private static final String TAG_CANCEL = "cancel";
30 |
31 | public TimePickerView(PickerOptions pickerOptions) {
32 | super(pickerOptions.context);
33 | mPickerOptions = pickerOptions;
34 | initView(pickerOptions.context);
35 | }
36 |
37 | private void initView(Context context) {
38 | setDialogOutSideCancelable();
39 | initViews();
40 | initAnim();
41 |
42 | if (mPickerOptions.customListener == null) {
43 | LayoutInflater.from(context).inflate(R.layout.pickerview_time, contentContainer);
44 |
45 | //顶部标题
46 | TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
47 | RelativeLayout rv_top_bar = (RelativeLayout) findViewById(R.id.rv_topbar);
48 |
49 | //确定和取消按钮
50 | Button btnSubmit = (Button) findViewById(R.id.btnSubmit);
51 | Button btnCancel = (Button) findViewById(R.id.btnCancel);
52 |
53 | btnSubmit.setTag(TAG_SUBMIT);
54 | btnCancel.setTag(TAG_CANCEL);
55 |
56 | btnSubmit.setOnClickListener(this);
57 | btnCancel.setOnClickListener(this);
58 |
59 | //设置文字
60 | btnSubmit.setText(TextUtils.isEmpty(mPickerOptions.textContentConfirm) ? context.getResources().getString(R.string.pickerview_submit) : mPickerOptions.textContentConfirm);
61 | btnCancel.setText(TextUtils.isEmpty(mPickerOptions.textContentCancel) ? context.getResources().getString(R.string.pickerview_cancel) : mPickerOptions.textContentCancel);
62 | tvTitle.setText(TextUtils.isEmpty(mPickerOptions.textContentTitle) ? "" : mPickerOptions.textContentTitle);//默认为空
63 |
64 | //设置color
65 | btnSubmit.setTextColor(mPickerOptions.textColorConfirm);
66 | btnCancel.setTextColor(mPickerOptions.textColorCancel);
67 | tvTitle.setTextColor(mPickerOptions.textColorTitle);
68 | rv_top_bar.setBackgroundColor(mPickerOptions.bgColorTitle);
69 |
70 | //设置文字大小
71 | btnSubmit.setTextSize(mPickerOptions.textSizeSubmitCancel);
72 | btnCancel.setTextSize(mPickerOptions.textSizeSubmitCancel);
73 | tvTitle.setTextSize(mPickerOptions.textSizeTitle);
74 |
75 | } else {
76 | mPickerOptions.customListener.customLayout(LayoutInflater.from(context).inflate(mPickerOptions.layoutRes, contentContainer));
77 | }
78 | // 时间转轮 自定义控件
79 | LinearLayout timePickerView = (LinearLayout) findViewById(R.id.timepicker);
80 | timePickerView.setBackgroundColor(mPickerOptions.bgColorWheel);
81 |
82 | initWheelTime(timePickerView);
83 | }
84 |
85 | private void initWheelTime(LinearLayout timePickerView) {
86 | wheelTime = new WheelTime(timePickerView, mPickerOptions.type, mPickerOptions.textGravity, mPickerOptions.textSizeContent);
87 | if (mPickerOptions.timeSelectChangeListener != null) {
88 | wheelTime.setSelectChangeCallback(new ISelectTimeCallback() {
89 | @Override
90 | public void onTimeSelectChanged() {
91 | try {
92 | Date date = WheelTime.dateFormat.parse(wheelTime.getTime());
93 | mPickerOptions.timeSelectChangeListener.onTimeSelectChanged(date);
94 | } catch (ParseException e) {
95 | e.printStackTrace();
96 | }
97 | }
98 | });
99 | }
100 |
101 | wheelTime.setHourList(mPickerOptions.hourList);
102 | wheelTime.setMinuteList(mPickerOptions.minuteList);
103 | wheelTime.setSecondList(mPickerOptions.secondList);
104 |
105 | wheelTime.setLunarMode(mPickerOptions.isLunarCalendar);
106 |
107 | if (mPickerOptions.startYear != 0 && mPickerOptions.endYear != 0
108 | && mPickerOptions.startYear <= mPickerOptions.endYear) {
109 | setRange();
110 | }
111 |
112 | //若手动设置了时间范围限制
113 | if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
114 | if (mPickerOptions.startDate.getTimeInMillis() > mPickerOptions.endDate.getTimeInMillis()) {
115 | throw new IllegalArgumentException("startDate can't be later than endDate");
116 | } else {
117 | setRangDate();
118 | }
119 | } else if (mPickerOptions.startDate != null) {
120 | if (mPickerOptions.startDate.get(Calendar.YEAR) < 1900) {
121 | throw new IllegalArgumentException("The startDate can not as early as 1900");
122 | } else {
123 | setRangDate();
124 | }
125 | } else if (mPickerOptions.endDate != null) {
126 | if (mPickerOptions.endDate.get(Calendar.YEAR) > 2100) {
127 | throw new IllegalArgumentException("The endDate should not be later than 2100");
128 | } else {
129 | setRangDate();
130 | }
131 | } else {//没有设置时间范围限制,则会使用默认范围。
132 | setRangDate();
133 | }
134 |
135 | setTime();
136 | wheelTime.setLabels(mPickerOptions.label_year, mPickerOptions.label_month, mPickerOptions.label_day
137 | , mPickerOptions.label_hours, mPickerOptions.label_minutes, mPickerOptions.label_seconds);
138 | wheelTime.setTextXOffset(mPickerOptions.x_offset_year, mPickerOptions.x_offset_month, mPickerOptions.x_offset_day,
139 | mPickerOptions.x_offset_hours, mPickerOptions.x_offset_minutes, mPickerOptions.x_offset_seconds);
140 | wheelTime.setItemsVisible(mPickerOptions.itemsVisibleCount);
141 | wheelTime.setAlphaGradient(mPickerOptions.isAlphaGradient);
142 | setOutSideCancelable(mPickerOptions.cancelable);
143 | wheelTime.setCyclic(mPickerOptions.cyclic);
144 | wheelTime.setDividerColor(mPickerOptions.dividerColor);
145 | wheelTime.setDividerType(mPickerOptions.dividerType);
146 | wheelTime.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier);
147 | wheelTime.setTextColorOut(mPickerOptions.textColorOut);
148 | wheelTime.setTextColorCenter(mPickerOptions.textColorCenter);
149 | wheelTime.isCenterLabel(mPickerOptions.isCenterLabel);
150 | }
151 |
152 |
153 | /**
154 | * 设置默认时间
155 | */
156 | public void setDate(Calendar date) {
157 | mPickerOptions.date = date;
158 | setTime();
159 | }
160 |
161 | /**
162 | * 设置可以选择的时间范围, 要在setTime之前调用才有效果
163 | */
164 | private void setRange() {
165 | wheelTime.setStartYear(mPickerOptions.startYear);
166 | wheelTime.setEndYear(mPickerOptions.endYear);
167 |
168 | }
169 |
170 | /**
171 | * 设置可以选择的时间范围, 要在setTime之前调用才有效果
172 | */
173 | private void setRangDate() {
174 | wheelTime.setRangDate(mPickerOptions.startDate, mPickerOptions.endDate);
175 | initDefaultSelectedDate();
176 | }
177 |
178 | private void initDefaultSelectedDate() {
179 | //如果手动设置了时间范围
180 | if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
181 | //若默认时间未设置,或者设置的默认时间越界了,则设置默认选中时间为开始时间。
182 | if (mPickerOptions.date == null || mPickerOptions.date.getTimeInMillis() < mPickerOptions.startDate.getTimeInMillis()
183 | || mPickerOptions.date.getTimeInMillis() > mPickerOptions.endDate.getTimeInMillis()) {
184 | mPickerOptions.date = mPickerOptions.startDate;
185 | }
186 | } else if (mPickerOptions.startDate != null) {
187 | //没有设置默认选中时间,那就拿开始时间当默认时间
188 | mPickerOptions.date = mPickerOptions.startDate;
189 | } else if (mPickerOptions.endDate != null) {
190 | mPickerOptions.date = mPickerOptions.endDate;
191 | }
192 | }
193 |
194 | /**
195 | * 设置选中时间,默认选中当前时间
196 | */
197 | private void setTime() {
198 | int year, month, day, hours, minute, seconds;
199 | Calendar calendar = Calendar.getInstance();
200 |
201 | if (mPickerOptions.date == null) {
202 | calendar.setTimeInMillis(System.currentTimeMillis());
203 | year = calendar.get(Calendar.YEAR);
204 | month = calendar.get(Calendar.MONTH);
205 | day = calendar.get(Calendar.DAY_OF_MONTH);
206 | hours = calendar.get(Calendar.HOUR_OF_DAY);
207 | minute = calendar.get(Calendar.MINUTE);
208 | seconds = calendar.get(Calendar.SECOND);
209 | } else {
210 | year = mPickerOptions.date.get(Calendar.YEAR);
211 | month = mPickerOptions.date.get(Calendar.MONTH);
212 | day = mPickerOptions.date.get(Calendar.DAY_OF_MONTH);
213 | hours = mPickerOptions.date.get(Calendar.HOUR_OF_DAY);
214 | minute = mPickerOptions.date.get(Calendar.MINUTE);
215 | seconds = mPickerOptions.date.get(Calendar.SECOND);
216 | }
217 |
218 | wheelTime.setPicker(year, month, day, hours, minute, seconds);
219 | }
220 |
221 |
222 | @Override
223 | public void onClick(View v) {
224 | String tag = (String) v.getTag();
225 | if (tag.equals(TAG_SUBMIT)) {
226 | returnData();
227 | } else if (tag.equals(TAG_CANCEL)) {
228 | if (mPickerOptions.cancelListener != null) {
229 | mPickerOptions.cancelListener.onClick(v);
230 | }
231 | }
232 | dismiss();
233 | }
234 |
235 | public void returnData() {
236 | if (mPickerOptions.timeSelectListener != null) {
237 | try {
238 | Date date = WheelTime.dateFormat.parse(wheelTime.getTime());
239 | mPickerOptions.timeSelectListener.onTimeSelect(date, clickView);
240 | } catch (ParseException e) {
241 | e.printStackTrace();
242 | }
243 | }
244 | }
245 |
246 | /**
247 | * 动态设置标题
248 | *
249 | * @param text 标题文本内容
250 | */
251 | public void setTitleText(String text) {
252 | TextView tvTitle = (TextView) findViewById(R.id.tvTitle);
253 | if (tvTitle != null) {
254 | tvTitle.setText(text);
255 | }
256 | }
257 |
258 | /**
259 | * 目前暂时只支持设置1900 - 2100年
260 | *
261 | * @param lunar 农历的开关
262 | */
263 | public void setLunarCalendar(boolean lunar) {
264 | try {
265 | int year, month, day, hours, minute, seconds;
266 | Calendar calendar = Calendar.getInstance();
267 | calendar.setTime(WheelTime.dateFormat.parse(wheelTime.getTime()));
268 | year = calendar.get(Calendar.YEAR);
269 | month = calendar.get(Calendar.MONTH);
270 | day = calendar.get(Calendar.DAY_OF_MONTH);
271 | hours = calendar.get(Calendar.HOUR_OF_DAY);
272 | minute = calendar.get(Calendar.MINUTE);
273 | seconds = calendar.get(Calendar.SECOND);
274 |
275 | wheelTime.setLunarMode(lunar);
276 | wheelTime.setLabels(mPickerOptions.label_year, mPickerOptions.label_month, mPickerOptions.label_day,
277 | mPickerOptions.label_hours, mPickerOptions.label_minutes, mPickerOptions.label_seconds);
278 | wheelTime.setPicker(year, month, day, hours, minute, seconds);
279 | } catch (ParseException e) {
280 | e.printStackTrace();
281 | }
282 | }
283 |
284 | public boolean isLunarCalendar() {
285 | return wheelTime.isLunarMode();
286 | }
287 |
288 |
289 | @Override
290 | public boolean isDialog() {
291 | return mPickerOptions.isDialog;
292 | }
293 | }
294 |
--------------------------------------------------------------------------------
/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelOptions.java:
--------------------------------------------------------------------------------
1 | package com.bigkoo.pickerview.view;
2 |
3 | import android.graphics.Typeface;
4 | import android.view.View;
5 |
6 | import com.bigkoo.pickerview.R;
7 | import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
8 | import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
9 | import com.contrarywind.listener.OnItemSelectedListener;
10 | import com.contrarywind.view.WheelView;
11 |
12 | import java.util.List;
13 |
14 | public class WheelOptions {
15 | private View view;
16 | private WheelView wv_option1;
17 | private WheelView wv_option2;
18 | private WheelView wv_option3;
19 |
20 | private List mOptions1Items;
21 | private List> mOptions2Items;
22 | private List>> mOptions3Items;
23 |
24 | private boolean linkage = true;//默认联动
25 | private boolean isRestoreItem; //切换时,还原第一项
26 | private OnItemSelectedListener wheelListener_option1;
27 | private OnItemSelectedListener wheelListener_option2;
28 |
29 | private OnOptionsSelectChangeListener optionsSelectChangeListener;
30 |
31 | public View getView() {
32 | return view;
33 | }
34 |
35 | public void setView(View view) {
36 | this.view = view;
37 | }
38 |
39 | public WheelOptions(View view, boolean isRestoreItem) {
40 | super();
41 | this.isRestoreItem = isRestoreItem;
42 | this.view = view;
43 | wv_option1 = (WheelView) view.findViewById(R.id.options1);// 初始化时显示的数据
44 | wv_option2 = (WheelView) view.findViewById(R.id.options2);
45 | wv_option3 = (WheelView) view.findViewById(R.id.options3);
46 | }
47 |
48 |
49 | public void setPicker(List options1Items,
50 | List> options2Items,
51 | List>> options3Items) {
52 | this.mOptions1Items = options1Items;
53 | this.mOptions2Items = options2Items;
54 | this.mOptions3Items = options3Items;
55 |
56 | // 选项1
57 | wv_option1.setAdapter(new ArrayWheelAdapter(mOptions1Items));// 设置显示数据
58 | wv_option1.setCurrentItem(0);// 初始化时显示的数据
59 | // 选项2
60 | if (mOptions2Items != null) {
61 | wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(0)));// 设置显示数据
62 | }
63 | wv_option2.setCurrentItem(wv_option2.getCurrentItem());// 初始化时显示的数据
64 | // 选项3
65 | if (mOptions3Items != null) {
66 | wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(0).get(0)));// 设置显示数据
67 | }
68 | wv_option3.setCurrentItem(wv_option3.getCurrentItem());
69 | wv_option1.setIsOptions(true);
70 | wv_option2.setIsOptions(true);
71 | wv_option3.setIsOptions(true);
72 |
73 | if (this.mOptions2Items == null) {
74 | wv_option2.setVisibility(View.GONE);
75 | } else {
76 | wv_option2.setVisibility(View.VISIBLE);
77 | }
78 | if (this.mOptions3Items == null) {
79 | wv_option3.setVisibility(View.GONE);
80 | } else {
81 | wv_option3.setVisibility(View.VISIBLE);
82 | }
83 |
84 | // 联动监听器
85 | wheelListener_option1 = new OnItemSelectedListener() {
86 |
87 | @Override
88 | public void onItemSelected(int index) {
89 | int opt2Select = 0;
90 | if (mOptions2Items == null) {//只有1级联动数据
91 | if (optionsSelectChangeListener != null) {
92 | optionsSelectChangeListener.onOptionsSelectChanged(wv_option1.getCurrentItem(), 0, 0);
93 | }
94 | } else {
95 | if (!isRestoreItem) {
96 | opt2Select = wv_option2.getCurrentItem();//上一个opt2的选中位置
97 | //新opt2的位置,判断如果旧位置没有超过数据范围,则沿用旧位置,否则选中最后一项
98 | opt2Select = opt2Select >= mOptions2Items.get(index).size() - 1 ? mOptions2Items.get(index).size() - 1 : opt2Select;
99 | }
100 | wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(index)));
101 | wv_option2.setCurrentItem(opt2Select);
102 |
103 | if (mOptions3Items != null) {
104 | wheelListener_option2.onItemSelected(opt2Select);
105 | } else {//只有2级联动数据,滑动第1项回调
106 | if (optionsSelectChangeListener != null) {
107 | optionsSelectChangeListener.onOptionsSelectChanged(index, opt2Select, 0);
108 | }
109 | }
110 | }
111 | }
112 | };
113 |
114 | wheelListener_option2 = new OnItemSelectedListener() {
115 |
116 | @Override
117 | public void onItemSelected(int index) {
118 | if (mOptions3Items != null) {
119 | int opt1Select = wv_option1.getCurrentItem();
120 | opt1Select = opt1Select >= mOptions3Items.size() - 1 ? mOptions3Items.size() - 1 : opt1Select;
121 | index = index >= mOptions2Items.get(opt1Select).size() - 1 ? mOptions2Items.get(opt1Select).size() - 1 : index;
122 | int opt3 = 0;
123 | if (!isRestoreItem) {
124 | // wv_option3.getCurrentItem() 上一个opt3的选中位置
125 | //新opt3的位置,判断如果旧位置没有超过数据范围,则沿用旧位置,否则选中最后一项
126 | opt3 = wv_option3.getCurrentItem() >= mOptions3Items.get(opt1Select).get(index).size() - 1 ?
127 | mOptions3Items.get(opt1Select).get(index).size() - 1 : wv_option3.getCurrentItem();
128 | }
129 | wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(wv_option1.getCurrentItem()).get(index)));
130 | wv_option3.setCurrentItem(opt3);
131 |
132 | //3级联动数据实时回调
133 | if (optionsSelectChangeListener != null) {
134 | optionsSelectChangeListener.onOptionsSelectChanged(wv_option1.getCurrentItem(), index, opt3);
135 | }
136 | } else {//只有2级联动数据,滑动第2项回调
137 | if (optionsSelectChangeListener != null) {
138 | optionsSelectChangeListener.onOptionsSelectChanged(wv_option1.getCurrentItem(), index, 0);
139 | }
140 | }
141 | }
142 | };
143 |
144 | // 添加联动监听
145 | if (options1Items != null && linkage) {
146 | wv_option1.setOnItemSelectedListener(wheelListener_option1);
147 | }
148 | if (options2Items != null && linkage) {
149 | wv_option2.setOnItemSelectedListener(wheelListener_option2);
150 | }
151 | if (options3Items != null && linkage && optionsSelectChangeListener != null) {
152 | wv_option3.setOnItemSelectedListener(new OnItemSelectedListener() {
153 | @Override
154 | public void onItemSelected(int index) {
155 | optionsSelectChangeListener.onOptionsSelectChanged(wv_option1.getCurrentItem(), wv_option2.getCurrentItem(), index);
156 | }
157 | });
158 | }
159 | }
160 |
161 |
162 | //不联动情况下
163 | public void setNPicker(List options1Items, List options2Items, List options3Items) {
164 |
165 | // 选项1
166 | wv_option1.setAdapter(new ArrayWheelAdapter<>(options1Items));// 设置显示数据
167 | wv_option1.setCurrentItem(0);// 初始化时显示的数据
168 | // 选项2
169 | if (options2Items != null) {
170 | wv_option2.setAdapter(new ArrayWheelAdapter<>(options2Items));// 设置显示数据
171 | }
172 | wv_option2.setCurrentItem(wv_option2.getCurrentItem());// 初始化时显示的数据
173 | // 选项3
174 | if (options3Items != null) {
175 | wv_option3.setAdapter(new ArrayWheelAdapter<>(options3Items));// 设置显示数据
176 | }
177 | wv_option3.setCurrentItem(wv_option3.getCurrentItem());
178 | wv_option1.setIsOptions(true);
179 | wv_option2.setIsOptions(true);
180 | wv_option3.setIsOptions(true);
181 |
182 | if (optionsSelectChangeListener != null) {
183 | wv_option1.setOnItemSelectedListener(new OnItemSelectedListener() {
184 | @Override
185 | public void onItemSelected(int index) {
186 | optionsSelectChangeListener.onOptionsSelectChanged(index, wv_option2.getCurrentItem(), wv_option3.getCurrentItem());
187 | }
188 | });
189 | }
190 |
191 | if (options2Items == null) {
192 | wv_option2.setVisibility(View.GONE);
193 | } else {
194 | wv_option2.setVisibility(View.VISIBLE);
195 | if (optionsSelectChangeListener != null) {
196 | wv_option2.setOnItemSelectedListener(new OnItemSelectedListener() {
197 | @Override
198 | public void onItemSelected(int index) {
199 | optionsSelectChangeListener.onOptionsSelectChanged(wv_option1.getCurrentItem(), index, wv_option3.getCurrentItem());
200 | }
201 | });
202 | }
203 | }
204 | if (options3Items == null) {
205 | wv_option3.setVisibility(View.GONE);
206 | } else {
207 | wv_option3.setVisibility(View.VISIBLE);
208 | if (optionsSelectChangeListener != null) {
209 | wv_option3.setOnItemSelectedListener(new OnItemSelectedListener() {
210 | @Override
211 | public void onItemSelected(int index) {
212 | optionsSelectChangeListener.onOptionsSelectChanged(wv_option1.getCurrentItem(), wv_option2.getCurrentItem(), index);
213 | }
214 | });
215 | }
216 | }
217 | }
218 |
219 | public void setTextContentSize(int textSize) {
220 | wv_option1.setTextSize(textSize);
221 | wv_option2.setTextSize(textSize);
222 | wv_option3.setTextSize(textSize);
223 | }
224 |
225 | private void setLineSpacingMultiplier() {
226 |
227 | }
228 |
229 | /**
230 | * 设置选项的单位
231 | *
232 | * @param label1 单位
233 | * @param label2 单位
234 | * @param label3 单位
235 | */
236 | public void setLabels(String label1, String label2, String label3) {
237 | if (label1 != null) {
238 | wv_option1.setLabel(label1);
239 | }
240 | if (label2 != null) {
241 | wv_option2.setLabel(label2);
242 | }
243 | if (label3 != null) {
244 | wv_option3.setLabel(label3);
245 | }
246 | }
247 |
248 | /**
249 | * 设置x轴偏移量
250 | */
251 | public void setTextXOffset(int x_offset_one, int x_offset_two, int x_offset_three) {
252 | wv_option1.setTextXOffset(x_offset_one);
253 | wv_option2.setTextXOffset(x_offset_two);
254 | wv_option3.setTextXOffset(x_offset_three);
255 | }
256 |
257 | /**
258 | * 设置是否循环滚动
259 | *
260 | * @param cyclic 是否循环
261 | */
262 | public void setCyclic(boolean cyclic) {
263 | wv_option1.setCyclic(cyclic);
264 | wv_option2.setCyclic(cyclic);
265 | wv_option3.setCyclic(cyclic);
266 | }
267 |
268 | /**
269 | * 设置字体样式
270 | *
271 | * @param font 系统提供的几种样式
272 | */
273 | public void setTypeface(Typeface font) {
274 | wv_option1.setTypeface(font);
275 | wv_option2.setTypeface(font);
276 | wv_option3.setTypeface(font);
277 | }
278 |
279 | /**
280 | * 分别设置第一二三级是否循环滚动
281 | *
282 | * @param cyclic1,cyclic2,cyclic3 是否循环
283 | */
284 | public void setCyclic(boolean cyclic1, boolean cyclic2, boolean cyclic3) {
285 | wv_option1.setCyclic(cyclic1);
286 | wv_option2.setCyclic(cyclic2);
287 | wv_option3.setCyclic(cyclic3);
288 | }
289 |
290 | /**
291 | * 返回当前选中的结果对应的位置数组 因为支持三级联动效果,分三个级别索引,0,1,2。
292 | * 在快速滑动未停止时,点击确定按钮,会进行判断,如果匹配数据越界,则设为0,防止index出错导致崩溃。
293 | *
294 | * @return 索引数组
295 | */
296 | public int[] getCurrentItems() {
297 | int[] currentItems = new int[3];
298 | currentItems[0] = wv_option1.getCurrentItem();
299 |
300 | if (mOptions2Items != null && mOptions2Items.size() > 0) {//非空判断
301 | currentItems[1] = wv_option2.getCurrentItem() > (mOptions2Items.get(currentItems[0]).size() - 1) ? 0 : wv_option2.getCurrentItem();
302 | } else {
303 | currentItems[1] = wv_option2.getCurrentItem();
304 | }
305 |
306 | if (mOptions3Items != null && mOptions3Items.size() > 0) {//非空判断
307 | currentItems[2] = wv_option3.getCurrentItem() > (mOptions3Items.get(currentItems[0]).get(currentItems[1]).size() - 1) ? 0 : wv_option3.getCurrentItem();
308 | } else {
309 | currentItems[2] = wv_option3.getCurrentItem();
310 | }
311 |
312 | return currentItems;
313 | }
314 |
315 | public void setCurrentItems(int option1, int option2, int option3) {
316 | if (linkage) {
317 | itemSelected(option1, option2, option3);
318 | } else {
319 | wv_option1.setCurrentItem(option1);
320 | wv_option2.setCurrentItem(option2);
321 | wv_option3.setCurrentItem(option3);
322 | }
323 | }
324 |
325 | private void itemSelected(int opt1Select, int opt2Select, int opt3Select) {
326 | if (mOptions1Items != null) {
327 | wv_option1.setCurrentItem(opt1Select);
328 | }
329 | if (mOptions2Items != null) {
330 | wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(opt1Select)));
331 | wv_option2.setCurrentItem(opt2Select);
332 | }
333 | if (mOptions3Items != null) {
334 | wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(opt1Select).get(opt2Select)));
335 | wv_option3.setCurrentItem(opt3Select);
336 | }
337 | }
338 |
339 | /**
340 | * 设置间距倍数,但是只能在1.2-4.0f之间
341 | *
342 | * @param lineSpacingMultiplier
343 | */
344 | public void setLineSpacingMultiplier(float lineSpacingMultiplier) {
345 | wv_option1.setLineSpacingMultiplier(lineSpacingMultiplier);
346 | wv_option2.setLineSpacingMultiplier(lineSpacingMultiplier);
347 | wv_option3.setLineSpacingMultiplier(lineSpacingMultiplier);
348 | }
349 |
350 | /**
351 | * 设置分割线的颜色
352 | *
353 | * @param dividerColor
354 | */
355 | public void setDividerColor(int dividerColor) {
356 | wv_option1.setDividerColor(dividerColor);
357 | wv_option2.setDividerColor(dividerColor);
358 | wv_option3.setDividerColor(dividerColor);
359 | }
360 |
361 | /**
362 | * 设置分割线的类型
363 | *
364 | * @param dividerType
365 | */
366 | public void setDividerType(WheelView.DividerType dividerType) {
367 | wv_option1.setDividerType(dividerType);
368 | wv_option2.setDividerType(dividerType);
369 | wv_option3.setDividerType(dividerType);
370 | }
371 |
372 | /**
373 | * 设置分割线之间的文字的颜色
374 | *
375 | * @param textColorCenter
376 | */
377 | public void setTextColorCenter(int textColorCenter) {
378 | wv_option1.setTextColorCenter(textColorCenter);
379 | wv_option2.setTextColorCenter(textColorCenter);
380 | wv_option3.setTextColorCenter(textColorCenter);
381 | }
382 |
383 | /**
384 | * 设置分割线以外文字的颜色
385 | *
386 | * @param textColorOut
387 | */
388 | public void setTextColorOut(int textColorOut) {
389 | wv_option1.setTextColorOut(textColorOut);
390 | wv_option2.setTextColorOut(textColorOut);
391 | wv_option3.setTextColorOut(textColorOut);
392 | }
393 |
394 | /**
395 | * Label 是否只显示中间选中项的
396 | *
397 | * @param isCenterLabel
398 | */
399 | public void isCenterLabel(boolean isCenterLabel) {
400 | wv_option1.isCenterLabel(isCenterLabel);
401 | wv_option2.isCenterLabel(isCenterLabel);
402 | wv_option3.isCenterLabel(isCenterLabel);
403 | }
404 |
405 | public void setOptionsSelectChangeListener(OnOptionsSelectChangeListener optionsSelectChangeListener) {
406 | this.optionsSelectChangeListener = optionsSelectChangeListener;
407 | }
408 |
409 | public void setLinkage(boolean linkage) {
410 | this.linkage = linkage;
411 | }
412 |
413 | /**
414 | * 设置最大可见数目
415 | *
416 | * @param itemsVisible 建议设置为 3 ~ 9之间。
417 | */
418 | public void setItemsVisible(int itemsVisible) {
419 | wv_option1.setItemsVisibleCount(itemsVisible);
420 | wv_option2.setItemsVisibleCount(itemsVisible);
421 | wv_option3.setItemsVisibleCount(itemsVisible);
422 | }
423 |
424 | public void setAlphaGradient(boolean isAlphaGradient) {
425 | wv_option1.setAlphaGradient(isAlphaGradient);
426 | wv_option2.setAlphaGradient(isAlphaGradient);
427 | wv_option3.setAlphaGradient(isAlphaGradient);
428 | }
429 | }
430 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/anim/pickerview_dialog_scale_in.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
18 |
19 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/anim/pickerview_dialog_scale_out.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
9 |
10 |
18 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/anim/pickerview_slide_in_bottom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/anim/pickerview_slide_out_bottom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
11 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/drawable/selector_pickerview_btn.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/layout/include_pickerview_topbar.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
19 |
20 |
30 |
31 |
43 |
44 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/layout/layout_basepickerview.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/layout/pickerview_options.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
20 |
21 |
26 |
27 |
32 |
33 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/layout/pickerview_time.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
20 |
21 |
26 |
27 |
32 |
33 |
38 |
39 |
44 |
45 |
50 |
51 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/values-en/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | Cancel
4 | Confirm
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 44dp
4 |
5 |
6 | 20dp
7 |
8 |
9 | 17sp
10 | 18sp
11 |
12 | 20sp
13 |
14 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/values/integers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 300
5 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 取消
4 | 确定
5 | 年
6 | 月
7 | 日
8 | 时
9 | 分
10 | 秒
11 |
12 |
--------------------------------------------------------------------------------
/pickerview/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
19 |
20 |
24 |
25 |
26 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/preview/CustomLayout.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/CustomLayout.gif
--------------------------------------------------------------------------------
/preview/JsonData.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/JsonData.gif
--------------------------------------------------------------------------------
/preview/Screen Shot 2017-11-09 at 4.25.02 PM.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/Screen Shot 2017-11-09 at 4.25.02 PM.png
--------------------------------------------------------------------------------
/preview/customHMS.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/customHMS.jpeg
--------------------------------------------------------------------------------
/preview/lunar.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/lunar.gif
--------------------------------------------------------------------------------
/preview/pickerdemo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/pickerdemo.gif
--------------------------------------------------------------------------------
/preview/pickerdemo1x.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/pickerdemo1x.gif
--------------------------------------------------------------------------------
/preview/pickerdemo_zhangshangshenghuo.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/pickerdemo_zhangshangshenghuo.gif
--------------------------------------------------------------------------------
/preview/timepicker.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/timepicker.gif
--------------------------------------------------------------------------------
/preview/timepicker_night.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/princekin-f/Android-PickerView/fa8839401072af247c42856640f7c52b6a662d04/preview/timepicker_night.gif
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':pickerview', ':wheelview'
2 |
--------------------------------------------------------------------------------
/wheelview/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/wheelview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | //apply plugin: 'com.novoda.bintray-release'//添加插件
4 |
5 |
6 |
7 | android {
8 | compileSdkVersion 26
9 |
10 | defaultConfig {
11 | minSdkVersion 14
12 | targetSdkVersion 26
13 | versionCode 31
14 | versionName "4.1.0"
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | lintOptions {
23 | abortOnError false
24 | }
25 | }
26 |
27 | //allprojects {
28 | // tasks.withType(Javadoc) {//兼容中文字符
29 | // options{
30 | // encoding "UTF-8"
31 | // charSet 'UTF-8'
32 | // links "http://docs.oracle.com/javase/7/docs/api"
33 | // }
34 | // }
35 | //}
36 | //publish {
37 | // userOrg = 'contrarywind'//bintray.com 用户名/组织名 user/org name
38 | // groupId = 'com.contrarywind'//JCenter上显示的路径 path
39 | // artifactId = 'wheelview'//项目名称 project name
40 | // publishVersion = '4.1.0'//版本号 version code
41 | // desc = 'this is a wheelview for android'//项目描述 description
42 | // website = 'https://github.com/Bigkoo/Android-PickerView' //项目网址链接 link
43 | //}
44 |
45 | dependencies {
46 | // compile fileTree(include: ['*.jar'], dir: 'libs')
47 | implementation 'com.android.support:support-annotations:28.0.0'
48 | }
--------------------------------------------------------------------------------
/wheelview/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in C:\Users\song\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/wheelview/src/androidTest/java/test/wheelview/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package test.wheelview;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("test.wheelview.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/wheelview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/adapter/WheelAdapter.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.adapter;
2 |
3 |
4 | public interface WheelAdapter {
5 | /**
6 | * Gets items count
7 | * @return the count of wheel items
8 | */
9 | int getItemsCount();
10 |
11 | /**
12 | * Gets a wheel item by index.
13 | * @param index the item index
14 | * @return the wheel item text or null
15 | */
16 | T getItem(int index);
17 |
18 | /**
19 | * Gets maximum item length. It is used to determine the wheel width.
20 | * If -1 is returned there will be used the default wheel width.
21 | * @param o the item object
22 | * @return the maximum item length or -1
23 | */
24 | int indexOf(T o);
25 | }
26 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/interfaces/IPickerViewData.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.interfaces;
2 |
3 | /**
4 | * Created by Sai on 2016/7/13.
5 | */
6 | public interface IPickerViewData {
7 | String getPickerViewText();
8 | }
9 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/listener/LoopViewGestureListener.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.listener;
2 |
3 | import android.view.MotionEvent;
4 |
5 | import com.contrarywind.view.WheelView;
6 |
7 |
8 | /**
9 | * 手势监听
10 | */
11 | public final class LoopViewGestureListener extends android.view.GestureDetector.SimpleOnGestureListener {
12 |
13 | private final WheelView wheelView;
14 |
15 |
16 | public LoopViewGestureListener(WheelView wheelView) {
17 | this.wheelView = wheelView;
18 | }
19 |
20 | @Override
21 | public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
22 | wheelView.scrollBy(velocityY);
23 | return true;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/listener/OnItemSelectedListener.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.listener;
2 |
3 |
4 | public interface OnItemSelectedListener {
5 | void onItemSelected(int index);
6 | }
7 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/timer/InertiaTimerTask.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.timer;
2 |
3 | import com.contrarywind.view.WheelView;
4 |
5 | import java.util.TimerTask;
6 |
7 | /**
8 | * 滚动惯性的实现
9 | *
10 | * @author 小嵩
11 | * date: 2017-12-23 23:20:44
12 | */
13 | public final class InertiaTimerTask extends TimerTask {
14 |
15 | private float mCurrentVelocityY; //当前滑动速度
16 | private final float mFirstVelocityY;//手指离开屏幕时的初始速度
17 | private final WheelView mWheelView;
18 |
19 | /**
20 | * @param wheelView 滚轮对象
21 | * @param velocityY Y轴滑行速度
22 | */
23 | public InertiaTimerTask(WheelView wheelView, float velocityY) {
24 | super();
25 | this.mWheelView = wheelView;
26 | this.mFirstVelocityY = velocityY;
27 | mCurrentVelocityY = Integer.MAX_VALUE;
28 | }
29 |
30 | @Override
31 | public final void run() {
32 |
33 | //防止闪动,对速度做一个限制。
34 | if (mCurrentVelocityY == Integer.MAX_VALUE) {
35 | if (Math.abs(mFirstVelocityY) > 2000F) {
36 | mCurrentVelocityY = mFirstVelocityY > 0 ? 2000F : -2000F;
37 | } else {
38 | mCurrentVelocityY = mFirstVelocityY;
39 | }
40 | }
41 |
42 | //发送handler消息 处理平顺停止滚动逻辑
43 | if (Math.abs(mCurrentVelocityY) >= 0.0F && Math.abs(mCurrentVelocityY) <= 20F) {
44 | mWheelView.cancelFuture();
45 | mWheelView.getHandler().sendEmptyMessage(MessageHandler.WHAT_SMOOTH_SCROLL);
46 | return;
47 | }
48 |
49 | int dy = (int) (mCurrentVelocityY / 100F);
50 | mWheelView.setTotalScrollY(mWheelView.getTotalScrollY() - dy);
51 | if (!mWheelView.isLoop()) {
52 | float itemHeight = mWheelView.getItemHeight();
53 | float top = (-mWheelView.getInitPosition()) * itemHeight;
54 | float bottom = (mWheelView.getItemsCount() - 1 - mWheelView.getInitPosition()) * itemHeight;
55 | if (mWheelView.getTotalScrollY() - itemHeight * 0.25 < top) {
56 | top = mWheelView.getTotalScrollY() + dy;
57 | } else if (mWheelView.getTotalScrollY() + itemHeight * 0.25 > bottom) {
58 | bottom = mWheelView.getTotalScrollY() + dy;
59 | }
60 |
61 | if (mWheelView.getTotalScrollY() <= top) {
62 | mCurrentVelocityY = 40F;
63 | mWheelView.setTotalScrollY((int) top);
64 | } else if (mWheelView.getTotalScrollY() >= bottom) {
65 | mWheelView.setTotalScrollY((int) bottom);
66 | mCurrentVelocityY = -40F;
67 | }
68 | }
69 |
70 | if (mCurrentVelocityY < 0.0F) {
71 | mCurrentVelocityY = mCurrentVelocityY + 20F;
72 | } else {
73 | mCurrentVelocityY = mCurrentVelocityY - 20F;
74 | }
75 |
76 | //刷新UI
77 | mWheelView.getHandler().sendEmptyMessage(MessageHandler.WHAT_INVALIDATE_LOOP_VIEW);
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/timer/MessageHandler.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.timer;
2 |
3 | import android.os.Handler;
4 | import android.os.Message;
5 |
6 | import com.contrarywind.view.WheelView;
7 |
8 | /**
9 | * Handler 消息类
10 | *
11 | * @author 小嵩
12 | * date: 2017-12-23 23:20:44
13 | */
14 | public final class MessageHandler extends Handler {
15 | public static final int WHAT_INVALIDATE_LOOP_VIEW = 1000;
16 | public static final int WHAT_SMOOTH_SCROLL = 2000;
17 | public static final int WHAT_ITEM_SELECTED = 3000;
18 |
19 | private final WheelView wheelView;
20 |
21 | public MessageHandler(WheelView wheelView) {
22 | this.wheelView = wheelView;
23 | }
24 |
25 | @Override
26 | public final void handleMessage(Message msg) {
27 | switch (msg.what) {
28 | case WHAT_INVALIDATE_LOOP_VIEW:
29 | wheelView.invalidate();
30 | break;
31 |
32 | case WHAT_SMOOTH_SCROLL:
33 | wheelView.smoothScroll(WheelView.ACTION.FLING);
34 | break;
35 |
36 | case WHAT_ITEM_SELECTED:
37 | wheelView.onItemSelected();
38 | break;
39 | }
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/wheelview/src/main/java/com/contrarywind/timer/SmoothScrollTimerTask.java:
--------------------------------------------------------------------------------
1 | package com.contrarywind.timer;
2 |
3 | import com.contrarywind.view.WheelView;
4 |
5 | import java.util.TimerTask;
6 |
7 | /**
8 | * 平滑滚动的实现
9 | *
10 | * @author 小嵩
11 | */
12 | public final class SmoothScrollTimerTask extends TimerTask {
13 |
14 | private int realTotalOffset;
15 | private int realOffset;
16 | private int offset;
17 | private final WheelView wheelView;
18 |
19 | public SmoothScrollTimerTask(WheelView wheelView, int offset) {
20 | this.wheelView = wheelView;
21 | this.offset = offset;
22 | realTotalOffset = Integer.MAX_VALUE;
23 | realOffset = 0;
24 | }
25 |
26 | @Override
27 | public final void run() {
28 | if (realTotalOffset == Integer.MAX_VALUE) {
29 | realTotalOffset = offset;
30 | }
31 | //把要滚动的范围细分成10小份,按10小份单位来重绘
32 | realOffset = (int) ((float) realTotalOffset * 0.1F);
33 |
34 | if (realOffset == 0) {
35 | if (realTotalOffset < 0) {
36 | realOffset = -1;
37 | } else {
38 | realOffset = 1;
39 | }
40 | }
41 |
42 | if (Math.abs(realTotalOffset) <= 1) {
43 | wheelView.cancelFuture();
44 | wheelView.getHandler().sendEmptyMessage(MessageHandler.WHAT_ITEM_SELECTED);
45 | } else {
46 | wheelView.setTotalScrollY(wheelView.getTotalScrollY() + realOffset);
47 |
48 | //这里如果不是循环模式,则点击空白位置需要回滚,不然就会出现选到-1 item的 情况
49 | if (!wheelView.isLoop()) {
50 | float itemHeight = wheelView.getItemHeight();
51 | float top = (float) (-wheelView.getInitPosition()) * itemHeight;
52 | float bottom = (float) (wheelView.getItemsCount() - 1 - wheelView.getInitPosition()) * itemHeight;
53 | if (wheelView.getTotalScrollY() <= top || wheelView.getTotalScrollY() >= bottom) {
54 | wheelView.setTotalScrollY(wheelView.getTotalScrollY() - realOffset);
55 | wheelView.cancelFuture();
56 | wheelView.getHandler().sendEmptyMessage(MessageHandler.WHAT_ITEM_SELECTED);
57 | return;
58 | }
59 | }
60 | wheelView.getHandler().sendEmptyMessage(MessageHandler.WHAT_INVALIDATE_LOOP_VIEW);
61 | realTotalOffset = realTotalOffset - realOffset;
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/wheelview/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/wheelview/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #057dff
4 | #c2daf5
5 | #f5f5f5
6 |
7 | #000000
8 | #a8a8a8
9 | #2a2a2a
10 | #d5d5d5
11 | #60000000
12 | #FFFFFFFF
13 |
14 |
15 |
--------------------------------------------------------------------------------
/wheelview/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 44dp
4 |
5 |
6 | 20dp
7 |
8 |
9 | 17sp
10 | 18sp
11 |
12 | 20sp
13 |
14 |
--------------------------------------------------------------------------------
/wheelview/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | wheelview
3 |
4 |
--------------------------------------------------------------------------------