在JayFang1993 / DropDownMenu的基礎上進行修改,最後的到本身想要的效果android
原本的效果:git
而我最後實現的效果是:github
咱們先來分析一下DropDownMenu的源代碼ide
1.Menu下面多了一個橫線字體
2.點擊變換按鈕以後,按鈕下面的橫線以及字體,還有右邊的arrow都變成紅色,而且再次點擊同一個menu,仍然是紅色this
首先在menu_item中增長一個Viewspa
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修改code
增長變量blog
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 });