Skip to content

Commit 13e1aa6

Browse files
committed
code refactor
1 parent 3606831 commit 13e1aa6

File tree

1 file changed

+131
-92
lines changed

1 file changed

+131
-92
lines changed

app/src/main/java/com/yyy/djk/dropdownmenu/DropDownMenu.java

Lines changed: 131 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.support.annotation.NonNull;
56
import android.text.TextUtils;
67
import android.util.AttributeSet;
78
import android.util.DisplayMetrics;
8-
import android.util.Log;
99
import android.util.TypedValue;
1010
import android.view.Gravity;
1111
import android.view.View;
@@ -26,24 +26,34 @@
2626
*/
2727
public class DropDownMenu extends LinearLayout {
2828

29-
public static final String TAG = DropDownMenu.class.getSimpleName();
30-
31-
private LinearLayout navigateMenuView;
29+
//顶部菜单布局
30+
private LinearLayout tabMenuView;
31+
//底部容器,包含popupMenuViews,maskView
3232
private FrameLayout containerView;
33-
private FrameLayout coverView;
33+
//弹出菜单父布局
34+
private FrameLayout popupMenuViews;
35+
//遮罩半透明View,点击可关闭DropDownMenu
3436
private View maskView;
35-
private View currentView;
37+
//tabMenuView里面选中的tab位置,-1表示未选中
38+
private int current_tab_position = -1;
3639

40+
//分别是菜单进入,弹出,遮罩View进入,弹出动画
3741
private Animation dropdown_in, dropdown_out, dropdown_mask_in, dropdown_mask_out;
3842

39-
43+
//分割线颜色
4044
private int dividerColor = 0xffcccccc;
45+
//tab选中颜色
4146
private int textSelectedColor = 0xff890c85;
47+
//tab未选中颜色
4248
private int textUnselectedColor = 0xff111111;
49+
//遮罩颜色
4350
private int maskColor = 0x88888888;
51+
//tab字体大小
4452
private int menuTextSize = 14;
4553

54+
//tab选中图标
4655
private int menuSelectedIcon = R.mipmap.drop_down_selected_icon;
56+
//tab未选中图标
4757
private int menuUnselectedIcon = R.mipmap.drop_down_unselected_icon;
4858

4959

@@ -59,6 +69,8 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) {
5969
super(context, attrs, defStyleAttr);
6070

6171
setOrientation(VERTICAL);
72+
73+
//为DropDownMenu添加自定义属性
6274
int menuBackgroundColor = 0xffffffff;
6375
int underlineColor = 0xffcccccc;
6476
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownMenu);
@@ -69,67 +81,57 @@ public DropDownMenu(Context context, AttributeSet attrs, int defStyleAttr) {
6981
menuBackgroundColor = a.getColor(R.styleable.DropDownMenu_ddmenuBackgroundColor, menuBackgroundColor);
7082
maskColor = a.getColor(R.styleable.DropDownMenu_ddmaskColor, maskColor);
7183
menuTextSize = a.getDimensionPixelOffset(R.styleable.DropDownMenu_ddmenuTextSize, menuTextSize);
72-
7384
menuSelectedIcon = a.getResourceId(R.styleable.DropDownMenu_ddmenuSelectedIcon, menuSelectedIcon);
7485
menuUnselectedIcon = a.getResourceId(R.styleable.DropDownMenu_ddmenuUnselectedIcon, menuUnselectedIcon);
75-
7686
a.recycle();
7787

78-
navigateMenuView = new LinearLayout(context);
88+
//初始化tabMenuView并添加到tabMenuView
89+
tabMenuView = new LinearLayout(context);
7990
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);
8495

96+
//为tabMenuView添加下划线
8597
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)));
8799
underLine.setBackgroundColor(underlineColor);
88100
addView(underLine, 1);
89101

102+
//初始化containerView并将其添加到DropDownMenu
90103
containerView = new FrameLayout(context);
91104
containerView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
92105
addView(containerView, 2);
93106

107+
//初始化动画
94108
dropdown_in = AnimationUtils.loadAnimation(context, R.anim.dropdown_in);
95109
dropdown_out = AnimationUtils.loadAnimation(context, R.anim.dropdown_out);
96110
dropdown_mask_in = AnimationUtils.loadAnimation(context, R.anim.dropdown_mask_in);
97111
dropdown_mask_out = AnimationUtils.loadAnimation(context, R.anim.dropdown_mask_out);
98112

99-
100113
}
101114

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);
127129
}
128130
containerView.addView(contentView, 0);
129131

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);
133135

134136
maskView = new View(getContext());
135137
maskView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
@@ -140,89 +142,126 @@ public void onClick(View v) {
140142
closeMenu();
141143
}
142144
});
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);
147149
}
148150

149151
}
150152

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);
155169
}
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);
156178
}
157-
158179
}
159180

181+
/**
182+
* 改变tab文字
183+
*
184+
* @param text
185+
*/
186+
public void setTabText(String text) {
187+
((TextView) tabMenuView.getChildAt(current_tab_position)).setText(text);
188+
}
160189

190+
/**
191+
* 关闭菜单
192+
*/
161193
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) {
170205

171-
}
206+
}
172207

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+
}
178212

179-
@Override
180-
public void onAnimationRepeat(Animation animation) {
213+
@Override
214+
public void onAnimationRepeat(Animation animation) {
181215

182-
}
183-
});
184-
((TextView) navigateMenuView.getChildAt(i)).setTextColor(textUnselectedColor);
185-
((TextView) navigateMenuView.getChildAt(i)).setCompoundDrawablesWithIntrinsicBounds(null, null,
186-
getResources().getDrawable(menuUnselectedIcon), null);
187216
}
188-
}
217+
});
189218

190219
}
191220

221+
/**
222+
* DropDownMenu是否处于可见状态
223+
* @return
224+
*/
192225
public boolean isShowing() {
193-
return currentView != null;
226+
return current_tab_position != -1;
194227
}
195228

229+
/**
230+
* 切换菜单
231+
* @param target
232+
*/
196233
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) {
200238
closeMenu();
201239
} 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);
206242
maskView.clearAnimation();
207243
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);
210249
}
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,
213253
getResources().getDrawable(menuSelectedIcon), null);
214-
currentView = target;
215254
}
216255
} 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,
219258
getResources().getDrawable(menuUnselectedIcon), null);
220-
coverView.getChildAt(i / 2 + 1).setVisibility(View.GONE);
259+
popupMenuViews.getChildAt(i / 2 + 1).setVisibility(View.GONE);
221260
}
222261
}
223262
}
224263

225-
public int pxToDp(float value) {
264+
public int dpTpPx(float value) {
226265
DisplayMetrics dm = getResources().getDisplayMetrics();
227266
return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, dm) + 0.5);
228267
}

0 commit comments

Comments
 (0)