|
2 | 2 |
|
3 | 3 | import android.app.Activity; |
4 | 4 | import android.support.annotation.NonNull; |
| 5 | +import android.view.Gravity; |
5 | 6 | import android.view.View; |
| 7 | +import android.widget.LinearLayout; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import cn.qqtheme.framework.widget.WheelView; |
6 | 13 |
|
7 | 14 | /** |
8 | 15 | * 双项选择器,选择两项,数据不联动。 |
9 | | - * <p> |
| 16 | + * <p/> |
10 | 17 | * Author:李玉江[QQ:1032694760] |
11 | 18 | * DateTime:2017/5/1 8:34 |
12 | 19 | * Builder:Android Studio |
13 | 20 | */ |
14 | 21 | public class DoublePicker extends WheelPicker { |
| 22 | + private List<String> firstData = new ArrayList<>(); |
| 23 | + private List<String> secondData = new ArrayList<>(); |
| 24 | + private int selectedFirstIndex = 0; |
| 25 | + private int selectedSecondIndex = 0; |
| 26 | + private OnWheelListener onWheelListener; |
| 27 | + private OnPickListener onPickListener; |
15 | 28 |
|
16 | | - public DoublePicker(Activity activity) { |
| 29 | + public DoublePicker(Activity activity, List<String> firstData, List<String> secondData) { |
17 | 30 | super(activity); |
| 31 | + this.firstData = firstData; |
| 32 | + this.secondData = secondData; |
| 33 | + } |
| 34 | + |
| 35 | + public void setSelectedIndex(int firstIndex, int secondIndex) { |
| 36 | + if (firstIndex >= 0 && firstIndex < firstData.size()) { |
| 37 | + selectedFirstIndex = firstIndex; |
| 38 | + } |
| 39 | + if (secondIndex >= 0 && secondIndex < secondData.size()) { |
| 40 | + selectedSecondIndex = secondIndex; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public String getSelectedFirstItem() { |
| 45 | + if (firstData.size() > selectedFirstIndex) { |
| 46 | + return firstData.get(selectedFirstIndex); |
| 47 | + } |
| 48 | + return ""; |
| 49 | + } |
| 50 | + |
| 51 | + public String getSelectedSecondItem() { |
| 52 | + if (secondData.size() > selectedSecondIndex) { |
| 53 | + return secondData.get(selectedSecondIndex); |
| 54 | + } |
| 55 | + return ""; |
18 | 56 | } |
19 | 57 |
|
20 | 58 | @NonNull |
21 | 59 | @Override |
22 | 60 | protected View makeCenterView() { |
23 | | - throw new RuntimeException("Stub"); |
| 61 | + LinearLayout layout = new LinearLayout(activity); |
| 62 | + layout.setOrientation(LinearLayout.HORIZONTAL); |
| 63 | + layout.setGravity(Gravity.CENTER); |
| 64 | + final WheelView firstView = createWheelView(); |
| 65 | + firstView.setLayoutParams(new LinearLayout.LayoutParams(0, WRAP_CONTENT, 1.0f)); |
| 66 | + layout.addView(firstView); |
| 67 | + final WheelView secondView = createWheelView(); |
| 68 | + secondView.setLayoutParams(new LinearLayout.LayoutParams(0, WRAP_CONTENT, 1.0f)); |
| 69 | + layout.addView(secondView); |
| 70 | + firstView.setItems(firstData, selectedFirstIndex); |
| 71 | + firstView.setOnItemSelectListener(new WheelView.OnItemSelectListener() { |
| 72 | + @Override |
| 73 | + public void onSelected(int index) { |
| 74 | + selectedFirstIndex = index; |
| 75 | + if (onWheelListener != null) { |
| 76 | + onWheelListener.onFirstWheeled(selectedFirstIndex, firstData.get(selectedFirstIndex)); |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + secondView.setItems(secondData, selectedSecondIndex); |
| 81 | + secondView.setOnItemSelectListener(new WheelView.OnItemSelectListener() { |
| 82 | + @Override |
| 83 | + public void onSelected(int index) { |
| 84 | + selectedSecondIndex = index; |
| 85 | + if (onWheelListener != null) { |
| 86 | + onWheelListener.onSecondWheeled(selectedSecondIndex, secondData.get(selectedSecondIndex)); |
| 87 | + } |
| 88 | + } |
| 89 | + }); |
| 90 | + return layout; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void onSubmit() { |
| 95 | + if (onPickListener != null) { |
| 96 | + onPickListener.onPicked(selectedFirstIndex, selectedSecondIndex); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public void setOnWheelListener(OnWheelListener onWheelListener) { |
| 101 | + this.onWheelListener = onWheelListener; |
| 102 | + } |
| 103 | + |
| 104 | + public void setOnPickListener(OnPickListener onPickListener) { |
| 105 | + this.onPickListener = onPickListener; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * 数据条目滑动监听器 |
| 110 | + */ |
| 111 | + public interface OnWheelListener { |
| 112 | + |
| 113 | + void onFirstWheeled(int index, String item); |
| 114 | + |
| 115 | + void onSecondWheeled(int index, String item); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * 数据选择完成监听器 |
| 121 | + */ |
| 122 | + public interface OnPickListener { |
| 123 | + |
| 124 | + void onPicked(int selectedFirstIndex, int selectedSecondIndex); |
| 125 | + |
24 | 126 | } |
25 | 127 |
|
26 | 128 | } |
0 commit comments