在JayFang1993 / DropDownMenu的基础上进行修改,最后的到自己想要的效果
本来的效果:
而我最后实现的效果是:
我们先来分析一下DropDownMenu的源代码
需要改动的地方
1.Menu下面多了一个横线
2.点击变换按钮之后,按钮下面的横线以及字体,还有右边的arrow都变成红色,并且再次点击同一个menu,仍然是红色
解决横线问题
首先在menu_item中增加一个View
<View
android:id="@+id/menu_divider"
android:layout_width="35dp"
android:layout_height="4dp"
android:layout_alignLeft="@id/tv_menu_title"
android:layout_alignRight="@id/tv_menu_title"
android:layout_below="@id/tv_menu_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="4dp"
android:background="@color/divider_menu"
android:visibility="invisible" />
在DropDownMenu修改
增加变量
private boolean mShowMenuDivider;
在init方法中设置mShowMenuDivider = true;
为了当Menu点击选项变化时,改变字体颜色和显示横线,我重写了OnMenuSelectedListener用于监听选择的主Menu改变事件,又增加了一个OnMenuItemSelectedListener监听器,用于监听Menu展开后的点击Item事件
public interface OnMenuSelectedListener {
public void OnMenuChanged(List<ImageView> mIvMenuArrow, View view, int nowRowIndex); }
本来我没有加mIvMenuArrow这个参数的,后面会讲为什么增加这个参数
在MainActivity中实现OnMenuSelectedListener接口,重写onMenuChanged方法
观察以下代码
通过循环画出menu的选项页面,并调用监听方法
for (int i = 0; i < mMenuCount; i++) {
// final View menuDivider;
final RelativeLayout v = (RelativeLayout) LayoutInflater.from(
mContext).inflate(R.layout.menu_item, null, false);
// menuDivider = v.findViewById(R.id.menu_divider); RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
width / mMenuCount, LayoutParams.WRAP_CONTENT);
v.setLayoutParams(parms);
TextView tv = (TextView) v.findViewById(R.id.tv_menu_title);
tv.setTextColor(mMenuTitleTextColor);
tv.setTextSize(mMenuTitleTextSize);
if (mDefaultMenuTitle == null || mDefaultMenuTitle.length == 0) {
tv.setText(mMenuItems.get(i)[0]);
} else {
tv.setText(mDefaultMenuTitle[i]);
}
this.addView(v, i);
mTvMenuTitles.add(tv); RelativeLayout rl = (RelativeLayout) v
.findViewById(R.id.rl_menu_head);
rl.setBackgroundColor(mMenuBackColor);
mRlMenuBacks.add(rl); ImageView iv = (ImageView) v.findViewById(R.id.iv_menu_arrow);
mIvMenuArrow.add(iv);
mIvMenuArrow.get(i).setImageResource(mArrow.get(i).get("down"));
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv
.getLayoutParams();
params.leftMargin = mArrowMarginTitle;
iv.setLayoutParams(params); final int index = i;
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.i("tanjin", "DropDwonMenu------->OnClick");
mMenuList.setAdapter(mMenuAdapters.get(index));
if (mMenuAdapters.get(index).getCount() > mShowCount) {
View childView = mMenuAdapters.get(index).getView(
0, null, mMenuList);
childView.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, childView
.getMeasuredHeight() * mShowCount);
mMenuList.setLayoutParams(parms);
} else {
View childView = mMenuAdapters.get(index).getView(
0, null, mMenuList);
childView.measure(MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
mMenuList.setLayoutParams(parms);
}
if (mMenuSelectedListener == null && isDebug)
Toast.makeText(mContext,
"MenuSelectedListener is null",
Toast.LENGTH_LONG).show();
else {
//调用监听方法
mMenuSelectedListener.OnMenuChanged(mIvMenuArrow,
view, index); }
if (!mShowDivider) {
mMenuList.setDivider(null);
}
mMenuList.setBackgroundColor(mMenuListBackColor);
mColumnSelected = index;
mTvMenuTitles.get(index).setTextColor(
mMenuPressedTitleTextColor);
mRlMenuBacks.get(index).setBackgroundColor(
mMenuPressedBackColor);
mIvMenuArrow.get(index).setImageResource(
mArrow.get(index).get("up"));
mPopupWindow.showAsDropDown(v); }
});
}
在Activity中设置监听器,并重写OnMenuChanged方法
menu.setMenuSelectedListener(new OnMenuSelectedListener() { private ArrayList<Map<String, Integer>> mArrow = new ArrayList<>(); @Override
public void OnMenuChanged(List<ImageView> mIvMenuArrow, View view,
int nowRowIndex) {
// TODO Auto-generated method stub
Log.i("tanjin", "MainActivity------>OnMenuChanged");
for (int i = 0; i < menu.getChildCount(); i++) { View v = menu.getChildAt(i);
v.findViewById(R.id.menu_divider).setVisibility(
View.INVISIBLE);
((TextView) v.findViewById(R.id.tv_menu_title))
.setTextColor(getResources().getColor(
R.color.default_menu_text));
if (i != nowRowIndex) {
mIvMenuArrow.get(i).setImageResource(
R.drawable.arrow_down);
} } menu.setmArrow(nowRowIndex);
view.findViewById(R.id.menu_divider)
.setVisibility(View.VISIBLE);
TextView tv = (TextView) view.findViewById(R.id.tv_menu_title);
tv.setTextColor(getResources().getColor(
R.color.default_menu_press_text)); Toast.makeText(CollectionActivity.this, " " + nowRowIndex,
Toast.LENGTH_SHORT).show(); }
});