自定义DropDownMenu菜单

 

在JayFang1993 / DropDownMenu的基础上进行修改,最后的到自己想要的效果

本来的效果:

 

 

而我最后实现的效果是:

自定义DropDownMenu菜单_第1张图片

自定义DropDownMenu菜单_第2张图片

自定义DropDownMenu菜单_第3张图片

 

我们先来分析一下DropDownMenu的源代码

 

自定义DropDownMenu菜单_第4张图片

 

需要改动的地方

1.Menu下面多了一个横线

2.点击变换按钮之后,按钮下面的横线以及字体,还有右边的arrow都变成红色,并且再次点击同一个menu,仍然是红色

 

解决横线问题

首先在menu_item中增加一个View

 

 1 <View
 2         android:id="@+id/menu_divider"
 3         android:layout_width="35dp"
 4         android:layout_height="4dp"
 5         android:layout_alignLeft="@id/tv_menu_title"
 6         android:layout_alignRight="@id/tv_menu_title"
 7         android:layout_below="@id/tv_menu_title"
 8         android:layout_centerHorizontal="true"
 9         android:layout_marginTop="4dp"
10         android:background="@color/divider_menu"
11         android:visibility="invisible" />

在DropDownMenu修改

增加变量

1 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方法

 

 1 menu.setMenuSelectedListener(new OnMenuSelectedListener() {
 2 
 3             private ArrayList<Map<String, Integer>> mArrow = new ArrayList<>();
 4 
 5             @Override
 6             public void OnMenuChanged(List<ImageView> mIvMenuArrow, View view,
 7                     int nowRowIndex) {
 8                 // TODO Auto-generated method stub
 9                 Log.i("tanjin", "MainActivity------>OnMenuChanged");
10                 for (int i = 0; i < menu.getChildCount(); i++) {
11 
12                     View v = menu.getChildAt(i);
13                     v.findViewById(R.id.menu_divider).setVisibility(
14                             View.INVISIBLE);
15                     ((TextView) v.findViewById(R.id.tv_menu_title))
16                             .setTextColor(getResources().getColor(
17                                     R.color.default_menu_text));
18                     if (i != nowRowIndex) {
19                         mIvMenuArrow.get(i).setImageResource(
20                                 R.drawable.arrow_down);
21                     }
22 
23                 }
24 
25                 menu.setmArrow(nowRowIndex);
26                 view.findViewById(R.id.menu_divider)
27                         .setVisibility(View.VISIBLE);
28                 TextView tv = (TextView) view.findViewById(R.id.tv_menu_title);
29                 tv.setTextColor(getResources().getColor(
30                         R.color.default_menu_press_text));
31 
32                 Toast.makeText(CollectionActivity.this, " " + nowRowIndex,
33                         Toast.LENGTH_SHORT).show();
34 
35             }
36         });

 

你可能感兴趣的:(自定义DropDownMenu菜单)