-
Notifications
You must be signed in to change notification settings - Fork 787
/
MainActivity.java
144 lines (122 loc) · 5.61 KB
/
MainActivity.java
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
package com.yyy.djk.dropdownmenu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import com.yyydjk.library.DropDownMenu;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
public class MainActivity extends AppCompatActivity {
@InjectView(R.id.dropDownMenu) DropDownMenu mDropDownMenu;
private String headers[] = {"城市", "年龄", "性别", "星座"};
private List<View> popupViews = new ArrayList<>();
private GirdDropDownAdapter cityAdapter;
private ListDropDownAdapter ageAdapter;
private ListDropDownAdapter sexAdapter;
private ConstellationAdapter constellationAdapter;
private String citys[] = {"不限", "武汉", "北京", "上海", "成都", "广州", "深圳", "重庆", "天津", "西安", "南京", "杭州"};
private String ages[] = {"不限", "18岁以下", "18-22岁", "23-26岁", "27-35岁", "35岁以上"};
private String sexs[] = {"不限", "男", "女"};
private String constellations[] = {"不限", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座", "水瓶座", "双鱼座"};
private int constellationPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
initView();
}
private void initView() {
//init city menu
final ListView cityView = new ListView(this);
cityAdapter = new GirdDropDownAdapter(this, Arrays.asList(citys));
cityView.setDividerHeight(0);
cityView.setAdapter(cityAdapter);
//init age menu
final ListView ageView = new ListView(this);
ageView.setDividerHeight(0);
ageAdapter = new ListDropDownAdapter(this, Arrays.asList(ages));
ageView.setAdapter(ageAdapter);
//init sex menu
final ListView sexView = new ListView(this);
sexView.setDividerHeight(0);
sexAdapter = new ListDropDownAdapter(this, Arrays.asList(sexs));
sexView.setAdapter(sexAdapter);
//init constellation
final View constellationView = getLayoutInflater().inflate(R.layout.custom_layout, null);
GridView constellation = ButterKnife.findById(constellationView, R.id.constellation);
constellationAdapter = new ConstellationAdapter(this, Arrays.asList(constellations));
constellation.setAdapter(constellationAdapter);
TextView ok = ButterKnife.findById(constellationView, R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDropDownMenu.setTabText(constellationPosition == 0 ? headers[3] : constellations[constellationPosition]);
mDropDownMenu.closeMenu();
}
});
//init popupViews
popupViews.add(cityView);
popupViews.add(ageView);
popupViews.add(sexView);
popupViews.add(constellationView);
//add item click event
cityView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cityAdapter.setCheckItem(position);
mDropDownMenu.setTabText(position == 0 ? headers[0] : citys[position]);
mDropDownMenu.closeMenu();
}
});
ageView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ageAdapter.setCheckItem(position);
mDropDownMenu.setTabText(position == 0 ? headers[1] : ages[position]);
mDropDownMenu.closeMenu();
}
});
sexView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sexAdapter.setCheckItem(position);
mDropDownMenu.setTabText(position == 0 ? headers[2] : sexs[position]);
mDropDownMenu.closeMenu();
}
});
constellation.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
constellationAdapter.setCheckItem(position);
constellationPosition = position;
}
});
//init context view
TextView contentView = new TextView(this);
contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
contentView.setText("内容显示区域");
contentView.setGravity(Gravity.CENTER);
contentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
//init dropdownview
mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
}
@Override
public void onBackPressed() {
//退出activity前关闭菜单
if (mDropDownMenu.isShowing()) {
mDropDownMenu.closeMenu();
} else {
super.onBackPressed();
}
}
}