|
| 1 | +package cn.qqtheme.framework.picker; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import android.view.Gravity; |
| 6 | +import android.widget.CheckBox; |
| 7 | +import android.widget.LinearLayout; |
| 8 | +import android.widget.ScrollView; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.Arrays; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +import cn.qqtheme.framework.popup.ConfirmPopup; |
| 16 | + |
| 17 | +/** |
| 18 | + * 多项选择器 |
| 19 | + * |
| 20 | + * @author 李玉江[QQ:1032694760] |
| 21 | + * @since 2017/06/24 |
| 22 | + */ |
| 23 | +public class MultiplePicker extends ConfirmPopup<ScrollView> { |
| 24 | + private List<String> items = new ArrayList<>(); |
| 25 | + private LinearLayout layout; |
| 26 | + private OnItemPickListener onItemPickListener; |
| 27 | + |
| 28 | + public MultiplePicker(Activity activity, String[] items) { |
| 29 | + this(activity, Arrays.asList(items)); |
| 30 | + } |
| 31 | + |
| 32 | + public MultiplePicker(Activity activity, List<String> items) { |
| 33 | + super(activity); |
| 34 | + this.items = items; |
| 35 | + } |
| 36 | + |
| 37 | + public void setOnItemPickListener(OnItemPickListener onItemPickListener) { |
| 38 | + this.onItemPickListener = onItemPickListener; |
| 39 | + } |
| 40 | + |
| 41 | + @NonNull |
| 42 | + @Override |
| 43 | + protected ScrollView makeCenterView() { |
| 44 | + ScrollView scrollView = new ScrollView(activity); |
| 45 | + layout = new LinearLayout(activity); |
| 46 | + layout.setOrientation(LinearLayout.VERTICAL); |
| 47 | + for (String item : items) { |
| 48 | + LinearLayout line = new LinearLayout(activity); |
| 49 | + line.setOrientation(LinearLayout.HORIZONTAL); |
| 50 | + line.setGravity(Gravity.CENTER); |
| 51 | + TextView textView = new TextView(activity); |
| 52 | + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, WRAP_CONTENT, 1.0f); |
| 53 | + lp.gravity = Gravity.CENTER; |
| 54 | + textView.setLayoutParams(lp); |
| 55 | + textView.setText(item); |
| 56 | + textView.setGravity(Gravity.CENTER); |
| 57 | + line.addView(textView); |
| 58 | + CheckBox checkBox = new CheckBox(activity); |
| 59 | + checkBox.setLayoutParams(new LinearLayout.LayoutParams(0, WRAP_CONTENT, 0.4f)); |
| 60 | + line.addView(checkBox); |
| 61 | + layout.addView(line); |
| 62 | + } |
| 63 | + scrollView.addView(layout); |
| 64 | + return scrollView; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void onSubmit() { |
| 69 | + if (onItemPickListener == null) { |
| 70 | + return; |
| 71 | + } |
| 72 | + List<String> checked = new ArrayList<>(); |
| 73 | + for (int i = 0, count = layout.getChildCount(); i < count; i++) { |
| 74 | + LinearLayout line = (LinearLayout) layout.getChildAt(i); |
| 75 | + CheckBox checkBox = (CheckBox) line.getChildAt(1); |
| 76 | + if (checkBox.isChecked()) { |
| 77 | + TextView textView = (TextView) line.getChildAt(0); |
| 78 | + checked.add(textView.getText().toString()); |
| 79 | + } |
| 80 | + } |
| 81 | + onItemPickListener.onItemPicked(checked.size(), checked); |
| 82 | + } |
| 83 | + |
| 84 | + public interface OnItemPickListener { |
| 85 | + |
| 86 | + void onItemPicked(int count, List<String> items); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments