xuxiuxi
2017-04-01 3ba02088330d961fb8f65679eb76c98dc05da9a5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cn.com.basic.face.dialog;
 
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.bsk.zhangbo.demoforbsk.R;
import cn.com.basic.face.adapter.MyWheelAdapter;
 
import cn.com.basic.face.base.MainActivity;
import cn.com.basic.face.util.OkButtonClickedListener;
 
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.wx.wheelview.widget.WheelView;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
public class CountryDialog extends PopupWindow implements WheelView.OnWheelItemSelectedListener {
 
    @ViewInject(R.id.dialog_country_ok)
    private TextView dialog_country_ok;
    @ViewInject(R.id.dialog_country_cancel)
    private TextView dialog_country_cancel;
    @ViewInject(R.id.dialog_country_name_first_letter)
    private WheelView dialog_country_name_first_letter;
    @ViewInject(R.id.dialog_country_name)
    private WheelView dialog_country_name;
 
    private View view;
    private static HashMap<String, List<String>> countriesGroupByFirstLetter = new HashMap<>();
    private static List<String> uniqueFirstLetterList = new ArrayList<>();
    private List<String> countriesWithSameFirstLetter = new ArrayList<>();
 
    public static void setCountries(List<String> _uniqueFirstLetterList, HashMap<String, List<String>> _countriesGroupByFirstLetter) {
        uniqueFirstLetterList = _uniqueFirstLetterList;
        countriesGroupByFirstLetter = _countriesGroupByFirstLetter;
    }
 
    @OnClick(R.id.dialog_country_ok)
    public void dialog_country_ok_click(View view) {
        for (OkButtonClickedListener okButtonClickedListener : okButtonClickedListeners) {
            if (dialog_country_name_first_letter.getCurrentPosition() >= 0) {
                countriesWithSameFirstLetter = countriesGroupByFirstLetter.get(uniqueFirstLetterList.get(dialog_country_name_first_letter.getCurrentPosition()));
                okButtonClickedListener.onItemSelected(0, null, countriesWithSameFirstLetter.get(dialog_country_name.getCurrentPosition()));
            }
        }
        dismiss();
    }
 
    @OnClick(R.id.dialog_country_cancel)
    public void dialog_country_cancel_click(View view) {
        dismiss();
    }
 
    public CountryDialog(View parentView, OkButtonClickedListener okButtonClickedListener) {
        view = LayoutInflater.from(MainActivity.getInstance()).inflate(R.layout.dialog_country, null);
        ViewUtils.inject(this, view);
 
        this.countriesWithSameFirstLetter = countriesGroupByFirstLetter.get(uniqueFirstLetterList.get(dialog_country_name_first_letter.getSelection()));
 
        WheelView.WheelViewStyle wheelViewStyle = new WheelView.WheelViewStyle();
        wheelViewStyle.selectedTextColor = Color.parseColor("#11c3e3");
        wheelViewStyle.textColor = Color.parseColor("#bcc6cf");
        wheelViewStyle.selectedTextSize = 22;
        wheelViewStyle.textSize = 16;
        wheelViewStyle.holoBorderColor = Color.parseColor("#11c3e3");
 
        dialog_country_name_first_letter.setWheelAdapter(new MyWheelAdapter(MainActivity.getInstance()));
        dialog_country_name_first_letter.setSkin(WheelView.Skin.Holo);
        dialog_country_name_first_letter.setWheelSize(5);
        dialog_country_name_first_letter.setBackgroundResource(R.color.colorBackground);
        dialog_country_name_first_letter.setStyle(wheelViewStyle);
 
        dialog_country_name.setWheelAdapter(new MyWheelAdapter(MainActivity.getInstance()));
        dialog_country_name.setSkin(WheelView.Skin.Holo);
        dialog_country_name.setWheelSize(5);
        dialog_country_name.setBackgroundResource(R.color.colorBackground);
        dialog_country_name.setStyle(wheelViewStyle);
 
        initData();
 
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                View popLayout = view.findViewById(R.id.dialog_country_linear_layout);
                int top = popLayout.getTop();
                int bottom = popLayout.getBottom();
                int left = popLayout.getLeft();
                int right = popLayout.getRight();
                int y = (int) motionEvent.getY();
                int x = (int) motionEvent.getX();
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    if (y < top || y > bottom) {
                        dismiss();
                    }
                    if (x < left || x > right) {
                        dismiss();
                    }
 
                }
                return true;
            }
        });
 
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setContentView(this.view);
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setBackgroundDrawable(new ColorDrawable(0x7f000000));
        this.setAnimationStyle(R.style.PopupAnimation);
 
        okButtonClickedListeners.add(okButtonClickedListener);
        showAtLocation(parentView, Gravity.CENTER,0,0);
    }
 
    @Override
    public void onItemSelected(int position, Object o) {
        countriesWithSameFirstLetter = countriesGroupByFirstLetter.get(uniqueFirstLetterList.get(dialog_country_name_first_letter.getCurrentPosition()));
    }
 
    private void initData() {
        dialog_country_name_first_letter.setWheelData(uniqueFirstLetterList);
        dialog_country_name_first_letter.join(dialog_country_name);
        dialog_country_name_first_letter.joinDatas(countriesGroupByFirstLetter);
        dialog_country_name.setWheelData(countriesWithSameFirstLetter);
    }
 
    private List<OkButtonClickedListener> okButtonClickedListeners = new ArrayList<OkButtonClickedListener>();
 
    public void addOkButtonClickedListener(OkButtonClickedListener okButtonClickedListener) {
        this.okButtonClickedListeners.add(okButtonClickedListener);
    }
 
}