xuxiuxi
2017-03-31 d6e29e229c86b31004eff30093fca27c4ce3b3e7
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
package cn.com.basic.face.dialog;
 
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.AppCompatTextView;
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.util.OkButtonClickedListener;
 
import com.wx.wheelview.widget.WheelView;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
public class CountrySelectionDialog extends PopupWindow implements View.OnClickListener, WheelView.OnWheelItemSelectedListener {
 
    private Context mContext;
    private View view;
    private TextView mTvConfirm, mTvCancel;
    private WheelView mWheelViewTitle, mWheelViewName;
    private HashMap<String, List<String>> countryData;
    private List<String> countryWordList;
    private List<String> countryList;
 
    public CountrySelectionDialog(Context context, String[] countryWordData, List<String> countryWordList, HashMap<String, List<String>> countryData) {
 
        this.mContext = context;
        view = LayoutInflater.from(context).inflate(R.layout.dialog_country, null);
        mWheelViewTitle = (WheelView) view.findViewById(R.id.pop_country_title);
        mWheelViewTitle.setOnWheelItemSelectedListener(this);
        mWheelViewName = (WheelView) view.findViewById(R.id.pop_country_name);
        mWheelViewName.setOnWheelItemSelectedListener(this);
        mTvCancel = (TextView) view.findViewById(R.id.pop_country_cancel);
        mTvConfirm = (TextView) view.findViewById(R.id.pop_country_confirm);
        mTvCancel.setOnClickListener(this);
        mTvConfirm.setOnClickListener(this);
        this.countryWordList = countryWordList;
        this.countryData = countryData;
        countryList = countryData.get(countryWordList.get(mWheelViewTitle.getSelection()));
        mWheelViewTitle.setWheelAdapter(new MyWheelAdapter(context));
        mWheelViewTitle.setSkin(WheelView.Skin.Holo);
        mWheelViewTitle.setWheelSize(5);
        mWheelViewTitle.setBackgroundResource(R.color.colorBackground);
        mWheelViewName.setWheelAdapter(new MyWheelAdapter(context));
        mWheelViewName.setSkin(WheelView.Skin.Holo);
        mWheelViewName.setWheelSize(5);
        mWheelViewName.setBackgroundResource(R.color.colorBackground);
        WheelView.WheelViewStyle style = new WheelView.WheelViewStyle();
        style.selectedTextColor = Color.parseColor("#11c3e3");
        style.textColor = Color.parseColor("#bcc6cf");
        style.selectedTextSize = 22;
        style.textSize = 16;
        style.holoBorderColor = Color.parseColor("#11c3e3");
        mWheelViewTitle.setStyle(style);
        mWheelViewName.setStyle(style);
        initData();
        setOutsideTouchable(true);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                View popLayout = view.findViewById(R.id.pop_single_ll);
                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.setContentView(this.view);
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        ColorDrawable colorDrawable = new ColorDrawable(0x7f000000);
        this.setBackgroundDrawable(colorDrawable);
        this.setAnimationStyle(R.style.PopupAnimation);
    }
 
    @Override
    public void onClick(View view) {
        if (view instanceof AppCompatTextView) {
            if (((AppCompatTextView) view).getText().equals("确认")) {
                for (OkButtonClickedListener okButtonClickedListener : okButtonClickedListeners) {
                    if (mWheelViewTitle.getCurrentPosition() >= 0) {
                        countryList = countryData.get(countryWordList.get(mWheelViewTitle.getCurrentPosition()));
                        okButtonClickedListener.onItemSelected(0, null, countryList.get(mWheelViewName.getCurrentPosition()));
                    }
                }
                dismiss();
            }
            if (((AppCompatTextView) view).getText().equals("取消")) {
                dismiss();
            }
        }
    }
 
    @Override
    public void onItemSelected(int position, Object o) {
        countryList = countryData.get(countryWordList.get(mWheelViewTitle.getCurrentPosition()));
    }
 
    private void initData() {
        mWheelViewTitle.setWheelData(countryWordList);
        mWheelViewTitle.join(mWheelViewName);
        mWheelViewTitle.joinDatas(countryData);
        mWheelViewName.setWheelData(countryList);
    }
 
    private List<OkButtonClickedListener> okButtonClickedListeners = new ArrayList<OkButtonClickedListener>();
 
    public void addOkButtonClickedListener(OkButtonClickedListener okButtonClickedListener) {
        this.okButtonClickedListeners.add(okButtonClickedListener);
    }
 
}