2
2
3
3
import android .content .Context ;
4
4
import android .content .res .TypedArray ;
5
+ import android .support .annotation .NonNull ;
5
6
import android .text .TextUtils ;
6
7
import android .util .AttributeSet ;
7
8
import android .util .DisplayMetrics ;
8
- import android .util .Log ;
9
9
import android .util .TypedValue ;
10
10
import android .view .Gravity ;
11
11
import android .view .View ;
26
26
*/
27
27
public class DropDownMenu extends LinearLayout {
28
28
29
- public static final String TAG = DropDownMenu . class . getSimpleName ();
30
-
31
- private LinearLayout navigateMenuView ;
29
+ //顶部菜单布局
30
+ private LinearLayout tabMenuView ;
31
+ //底部容器,包含popupMenuViews,maskView
32
32
private FrameLayout containerView ;
33
- private FrameLayout coverView ;
33
+ //弹出菜单父布局
34
+ private FrameLayout popupMenuViews ;
35
+ //遮罩半透明View,点击可关闭DropDownMenu
34
36
private View maskView ;
35
- private View currentView ;
37
+ //tabMenuView里面选中的tab位置,-1表示未选中
38
+ private int current_tab_position = -1 ;
36
39
40
+ //分别是菜单进入,弹出,遮罩View进入,弹出动画
37
41
private Animation dropdown_in , dropdown_out , dropdown_mask_in , dropdown_mask_out ;
38
42
39
-
43
+ //分割线颜色
40
44
private int dividerColor = 0xffcccccc ;
45
+ //tab选中颜色
41
46
private int textSelectedColor = 0xff890c85 ;
47
+ //tab未选中颜色
42
48
private int textUnselectedColor = 0xff111111 ;
49
+ //遮罩颜色
43
50
private int maskColor = 0x88888888 ;
51
+ //tab字体大小
44
52
private int menuTextSize = 14 ;
45
53
54
+ //tab选中图标
46
55
private int menuSelectedIcon = R .mipmap .drop_down_selected_icon ;
56
+ //tab未选中图标
47
57
private int menuUnselectedIcon = R .mipmap .drop_down_unselected_icon ;
48
58
49
59
@@ -59,6 +69,8 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) {
59
69
super (context , attrs , defStyleAttr );
60
70
61
71
setOrientation (VERTICAL );
72
+
73
+ //为DropDownMenu添加自定义属性
62
74
int menuBackgroundColor = 0xffffffff ;
63
75
int underlineColor = 0xffcccccc ;
64
76
TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .DropDownMenu );
@@ -69,67 +81,57 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) {
69
81
menuBackgroundColor = a .getColor (R .styleable .DropDownMenu_ddmenuBackgroundColor , menuBackgroundColor );
70
82
maskColor = a .getColor (R .styleable .DropDownMenu_ddmaskColor , maskColor );
71
83
menuTextSize = a .getDimensionPixelOffset (R .styleable .DropDownMenu_ddmenuTextSize , menuTextSize );
72
-
73
84
menuSelectedIcon = a .getResourceId (R .styleable .DropDownMenu_ddmenuSelectedIcon , menuSelectedIcon );
74
85
menuUnselectedIcon = a .getResourceId (R .styleable .DropDownMenu_ddmenuUnselectedIcon , menuUnselectedIcon );
75
-
76
86
a .recycle ();
77
87
78
- navigateMenuView = new LinearLayout (context );
88
+ //初始化tabMenuView并添加到tabMenuView
89
+ tabMenuView = new LinearLayout (context );
79
90
LayoutParams params = new LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT );
80
- navigateMenuView .setOrientation (HORIZONTAL );
81
- navigateMenuView .setBackgroundColor (menuBackgroundColor );
82
- navigateMenuView .setLayoutParams (params );
83
- addView (navigateMenuView , 0 );
91
+ tabMenuView .setOrientation (HORIZONTAL );
92
+ tabMenuView .setBackgroundColor (menuBackgroundColor );
93
+ tabMenuView .setLayoutParams (params );
94
+ addView (tabMenuView , 0 );
84
95
96
+ //为tabMenuView添加下划线
85
97
View underLine = new View (getContext ());
86
- underLine .setLayoutParams (new LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , pxToDp (1.0f )));
98
+ underLine .setLayoutParams (new LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , dpTpPx (1.0f )));
87
99
underLine .setBackgroundColor (underlineColor );
88
100
addView (underLine , 1 );
89
101
102
+ //初始化containerView并将其添加到DropDownMenu
90
103
containerView = new FrameLayout (context );
91
104
containerView .setLayoutParams (new FrameLayout .LayoutParams (FrameLayout .LayoutParams .MATCH_PARENT , FrameLayout .LayoutParams .MATCH_PARENT ));
92
105
addView (containerView , 2 );
93
106
107
+ //初始化动画
94
108
dropdown_in = AnimationUtils .loadAnimation (context , R .anim .dropdown_in );
95
109
dropdown_out = AnimationUtils .loadAnimation (context , R .anim .dropdown_out );
96
110
dropdown_mask_in = AnimationUtils .loadAnimation (context , R .anim .dropdown_mask_in );
97
111
dropdown_mask_out = AnimationUtils .loadAnimation (context , R .anim .dropdown_mask_out );
98
112
99
-
100
113
}
101
114
102
- public void setDropDownMenu (List <String > texts , List <View > menus , View contentView ) {
103
- for (int i = 0 ; i < texts .size (); i ++) {
104
- final TextView menu = new TextView (getContext ());
105
- menu .setSingleLine ();
106
- menu .setEllipsize (TextUtils .TruncateAt .END );
107
- menu .setGravity (Gravity .CENTER );
108
- menu .setTextSize (TypedValue .COMPLEX_UNIT_SP ,menuTextSize );
109
- menu .setLayoutParams (new LayoutParams (0 , ViewGroup .LayoutParams .WRAP_CONTENT , 1.0f ));
110
- menu .setTextColor (textUnselectedColor );
111
- menu .setOnClickListener (new OnClickListener () {
112
- @ Override
113
- public void onClick (View v ) {
114
- switchMenu (menu );
115
- }
116
- });
117
- menu .setCompoundDrawablesWithIntrinsicBounds (null , null , getResources ().getDrawable (menuUnselectedIcon ), null );
118
- menu .setText (texts .get (i ));
119
- menu .setPadding (pxToDp (5 ), pxToDp (12 ), pxToDp (5 ), pxToDp (12 ));
120
- navigateMenuView .addView (menu );
121
- if (i < texts .size () - 1 ) {
122
- View view = new View (getContext ());
123
- view .setLayoutParams (new LayoutParams (pxToDp (0.5f ), ViewGroup .LayoutParams .MATCH_PARENT ));
124
- view .setBackgroundColor (dividerColor );
125
- navigateMenuView .addView (view );
126
- }
115
+ /**
116
+ * 初始化DropDownMenu
117
+ *
118
+ * @param tabTexts
119
+ * @param popupViews
120
+ * @param contentView
121
+ */
122
+ public void setDropDownMenu (@ NonNull List <String > tabTexts , @ NonNull List <View > popupViews , @ NonNull View contentView ) {
123
+ if (tabTexts .size () != popupViews .size ()) {
124
+ throw new IllegalArgumentException ("params not match, tabTexts.size() should be equal popupViews.size()" );
125
+ }
126
+
127
+ for (int i = 0 ; i < tabTexts .size (); i ++) {
128
+ addTab (tabTexts , i );
127
129
}
128
130
containerView .addView (contentView , 0 );
129
131
130
- coverView = new FrameLayout (getContext ());
131
- coverView .setVisibility (GONE );
132
- containerView .addView (coverView , 1 );
132
+ popupMenuViews = new FrameLayout (getContext ());
133
+ popupMenuViews .setVisibility (GONE );
134
+ containerView .addView (popupMenuViews , 1 );
133
135
134
136
maskView = new View (getContext ());
135
137
maskView .setLayoutParams (new FrameLayout .LayoutParams (FrameLayout .LayoutParams .MATCH_PARENT , FrameLayout .LayoutParams .MATCH_PARENT ));
@@ -140,89 +142,126 @@ public void onClick(View v) {
140
142
closeMenu ();
141
143
}
142
144
});
143
- coverView .addView (maskView , 0 );
144
- for (int i = 0 ; i < menus .size (); i ++) {
145
- menus .get (i ).setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
146
- coverView .addView (menus .get (i ), i + 1 );
145
+ popupMenuViews .addView (maskView , 0 );
146
+ for (int i = 0 ; i < popupViews .size (); i ++) {
147
+ popupViews .get (i ).setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
148
+ popupMenuViews .addView (popupViews .get (i ), i + 1 );
147
149
}
148
150
149
151
}
150
152
151
- public void setMenuText (View view , String text ) {
152
- for (int i = 1 ; i < coverView .getChildCount (); i ++) {
153
- if (view == coverView .getChildAt (i )) {
154
- ((TextView ) navigateMenuView .getChildAt (i * 2 - 2 )).setText (text );
153
+ private void addTab (@ NonNull List <String > tabTexts , int i ) {
154
+ final TextView tab = new TextView (getContext ());
155
+ tab .setSingleLine ();
156
+ tab .setEllipsize (TextUtils .TruncateAt .END );
157
+ tab .setGravity (Gravity .CENTER );
158
+ tab .setTextSize (TypedValue .COMPLEX_UNIT_SP , menuTextSize );
159
+ tab .setLayoutParams (new LayoutParams (0 , ViewGroup .LayoutParams .WRAP_CONTENT , 1.0f ));
160
+ tab .setTextColor (textUnselectedColor );
161
+ tab .setCompoundDrawablesWithIntrinsicBounds (null , null , getResources ().getDrawable (menuUnselectedIcon ), null );
162
+ tab .setText (tabTexts .get (i ));
163
+ tab .setPadding (dpTpPx (5 ), dpTpPx (12 ), dpTpPx (5 ), dpTpPx (12 ));
164
+ //添加点击事件
165
+ tab .setOnClickListener (new OnClickListener () {
166
+ @ Override
167
+ public void onClick (View v ) {
168
+ switchMenu (tab );
155
169
}
170
+ });
171
+ tabMenuView .addView (tab );
172
+ //添加分割线
173
+ if (i < tabTexts .size () - 1 ) {
174
+ View view = new View (getContext ());
175
+ view .setLayoutParams (new LayoutParams (dpTpPx (0.5f ), ViewGroup .LayoutParams .MATCH_PARENT ));
176
+ view .setBackgroundColor (dividerColor );
177
+ tabMenuView .addView (view );
156
178
}
157
-
158
179
}
159
180
181
+ /**
182
+ * 改变tab文字
183
+ *
184
+ * @param text
185
+ */
186
+ public void setTabText (String text ) {
187
+ ((TextView ) tabMenuView .getChildAt (current_tab_position )).setText (text );
188
+ }
160
189
190
+ /**
191
+ * 关闭菜单
192
+ */
161
193
public void closeMenu () {
162
- for (int i = 0 ; i < navigateMenuView .getChildCount (); i = i + 2 ) {
163
- if (currentView == navigateMenuView .getChildAt (i )) {
164
- coverView .getChildAt (i / 2 + 1 ).clearAnimation ();
165
- coverView .getChildAt (i / 2 + 1 ).startAnimation (dropdown_out );
166
- maskView .startAnimation (dropdown_mask_out );
167
- dropdown_out .setAnimationListener (new Animation .AnimationListener () {
168
- @ Override
169
- public void onAnimationStart (Animation animation ) {
194
+ ((TextView ) tabMenuView .getChildAt (current_tab_position )).setTextColor (textUnselectedColor );
195
+ ((TextView ) tabMenuView .getChildAt (current_tab_position )).setCompoundDrawablesWithIntrinsicBounds (null , null ,
196
+ getResources ().getDrawable (menuUnselectedIcon ), null );
197
+ popupMenuViews .getChildAt (current_tab_position / 2 + 1 ).clearAnimation ();
198
+ popupMenuViews .getChildAt (current_tab_position / 2 + 1 ).startAnimation (dropdown_out );
199
+ current_tab_position = -1 ;
200
+ maskView .startAnimation (dropdown_mask_out );
201
+
202
+ dropdown_out .setAnimationListener (new Animation .AnimationListener () {
203
+ @ Override
204
+ public void onAnimationStart (Animation animation ) {
170
205
171
- }
206
+ }
172
207
173
- @ Override
174
- public void onAnimationEnd (Animation animation ) {
175
- coverView .setVisibility (View .GONE );
176
- currentView = null ;
177
- }
208
+ @ Override
209
+ public void onAnimationEnd (Animation animation ) {
210
+ popupMenuViews .setVisibility (View .GONE );
211
+ }
178
212
179
- @ Override
180
- public void onAnimationRepeat (Animation animation ) {
213
+ @ Override
214
+ public void onAnimationRepeat (Animation animation ) {
181
215
182
- }
183
- });
184
- ((TextView ) navigateMenuView .getChildAt (i )).setTextColor (textUnselectedColor );
185
- ((TextView ) navigateMenuView .getChildAt (i )).setCompoundDrawablesWithIntrinsicBounds (null , null ,
186
- getResources ().getDrawable (menuUnselectedIcon ), null );
187
216
}
188
- }
217
+ });
189
218
190
219
}
191
220
221
+ /**
222
+ * DropDownMenu是否处于可见状态
223
+ * @return
224
+ */
192
225
public boolean isShowing () {
193
- return currentView != null ;
226
+ return current_tab_position != - 1 ;
194
227
}
195
228
229
+ /**
230
+ * 切换菜单
231
+ * @param target
232
+ */
196
233
private void switchMenu (View target ) {
197
- for (int i = 0 ; i < navigateMenuView .getChildCount (); i = i + 2 ) {
198
- if (target == navigateMenuView .getChildAt (i )) {
199
- if (currentView == navigateMenuView .getChildAt (i )) {
234
+ System .out .println (current_tab_position );
235
+ for (int i = 0 ; i < tabMenuView .getChildCount (); i = i + 2 ) {
236
+ if (target == tabMenuView .getChildAt (i )) {
237
+ if (current_tab_position == i ) {
200
238
closeMenu ();
201
239
} else {
202
- coverView .getChildAt (i / 2 + 1 ).setVisibility (View .VISIBLE );
203
- Log .i (TAG , String .valueOf (i ));
204
- if (coverView .getVisibility () == View .GONE ) {
205
- coverView .setVisibility (View .VISIBLE );
240
+ if (current_tab_position == -1 ) {
241
+ popupMenuViews .setVisibility (View .VISIBLE );
206
242
maskView .clearAnimation ();
207
243
maskView .startAnimation (dropdown_mask_in );
208
- coverView .getChildAt (i / 2 + 1 ).clearAnimation ();
209
- coverView .getChildAt (i / 2 + 1 ).startAnimation (dropdown_in );
244
+ popupMenuViews .getChildAt (i / 2 + 1 ).setVisibility (View .VISIBLE );
245
+ popupMenuViews .getChildAt (i / 2 + 1 ).clearAnimation ();
246
+ popupMenuViews .getChildAt (i / 2 + 1 ).startAnimation (dropdown_in );
247
+ } else {
248
+ popupMenuViews .getChildAt (i / 2 + 1 ).setVisibility (View .VISIBLE );
210
249
}
211
- ((TextView ) navigateMenuView .getChildAt (i )).setTextColor (textSelectedColor );
212
- ((TextView ) navigateMenuView .getChildAt (i )).setCompoundDrawablesWithIntrinsicBounds (null , null ,
250
+ current_tab_position = i ;
251
+ ((TextView ) tabMenuView .getChildAt (i )).setTextColor (textSelectedColor );
252
+ ((TextView ) tabMenuView .getChildAt (i )).setCompoundDrawablesWithIntrinsicBounds (null , null ,
213
253
getResources ().getDrawable (menuSelectedIcon ), null );
214
- currentView = target ;
215
254
}
216
255
} else {
217
- ((TextView ) navigateMenuView .getChildAt (i )).setTextColor (textUnselectedColor );
218
- ((TextView ) navigateMenuView .getChildAt (i )).setCompoundDrawablesWithIntrinsicBounds (null , null ,
256
+ ((TextView ) tabMenuView .getChildAt (i )).setTextColor (textUnselectedColor );
257
+ ((TextView ) tabMenuView .getChildAt (i )).setCompoundDrawablesWithIntrinsicBounds (null , null ,
219
258
getResources ().getDrawable (menuUnselectedIcon ), null );
220
- coverView .getChildAt (i / 2 + 1 ).setVisibility (View .GONE );
259
+ popupMenuViews .getChildAt (i / 2 + 1 ).setVisibility (View .GONE );
221
260
}
222
261
}
223
262
}
224
263
225
- public int pxToDp (float value ) {
264
+ public int dpTpPx (float value ) {
226
265
DisplayMetrics dm = getResources ().getDisplayMetrics ();
227
266
return (int ) (TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , value , dm ) + 0.5 );
228
267
}
0 commit comments