From 96d390f9c43bdbe1aea893b93cd8841d78f24fbf Mon Sep 17 00:00:00 2001 From: Jay Fang Date: Mon, 17 Aug 2015 11:07:54 +0800 Subject: [PATCH] optimize onDraw method and code style --- .../dropdownmenu/example/MainActivity.java | 64 +-- .../jayfang/dropdownmenu/DropDownMenu.java | 410 +++++++++--------- 2 files changed, 238 insertions(+), 236 deletions(-) diff --git a/example/src/main/java/com/jayfang/dropdownmenu/example/MainActivity.java b/example/src/main/java/com/jayfang/dropdownmenu/example/MainActivity.java index 203cb36..49bb07b 100644 --- a/example/src/main/java/com/jayfang/dropdownmenu/example/MainActivity.java +++ b/example/src/main/java/com/jayfang/dropdownmenu/example/MainActivity.java @@ -2,7 +2,6 @@ import android.graphics.Color; import android.os.Bundle; -import android.os.Handler; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.Menu; @@ -40,29 +39,29 @@ protected void onCreate(Bundle savedInstanceState) { mMenu=(DropDownMenu)findViewById(R.id.menu); - mMenu.setMenuCount(3); - mMenu.setShowCount(6); + mMenu.setmMenuCount(3); + mMenu.setmShowCount(6); mMenu.setShowCheck(true); - mMenu.setMenuTitleTextSize(16); - mMenu.setMenuTitleTextColor(Color.WHITE); - mMenu.setMenuListTextSize(16); - mMenu.setMenuListTextColor(Color.BLACK); - mMenu.setMenuBackColor(Color.GRAY); - mMenu.setMenuPressedBackColor(Color.WHITE); + mMenu.setmMenuTitleTextSize(16); + mMenu.setmMenuTitleTextColor(Color.parseColor("#777777")); + mMenu.setmMenuListTextSize(16); + mMenu.setmMenuListTextColor(Color.BLACK); + mMenu.setmMenuBackColor(Color.parseColor("#eeeeee")); + mMenu.setmMenuPressedBackColor(Color.WHITE); + mMenu.setmMenuPressedTitleTextColor(Color.BLACK); - mMenu.setCheckIcon(R.drawable.ico_make); + mMenu.setmCheckIcon(R.drawable.ico_make); - mMenu.setUpArrow(R.drawable.arrow_up); - mMenu.setDownArrow(R.drawable.arrow_down); + mMenu.setmUpArrow(R.drawable.arrow_up); + mMenu.setmDownArrow(R.drawable.arrow_down); mMenu.setDefaultMenuTitle(strings); mMenu.setShowDivider(false); - mMenu.setMenuListBackColor(getResources().getColor(R.color.white)); - mMenu.setMenuListSelectorRes(R.color.white); - mMenu.setArrowMarginTitle(20); - mMenu.setMenuPressedTitleTextColor(Color.BLACK); + mMenu.setmMenuListBackColor(getResources().getColor(R.color.white)); + mMenu.setmMenuListSelectorRes(R.color.white); + mMenu.setmArrowMarginTitle(20); mMenu.setMenuSelectedListener(new OnMenuSelectedListener() { @Override @@ -79,29 +78,30 @@ public void onSelected(View listview, int RowIndex, int ColumnIndex) { setFilter(); } }); -// List items = new ArrayList<>(); -// items.add(arr1); -// items.add(arr2); -// items.add(arr3); -// mMenu.setMenuItems(items); - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - List items = new ArrayList<>(); - items.add(arr1); - items.add(arr2); - items.add(arr3); - mMenu.setMenuItems(items); + List items = new ArrayList<>(); + items.add(arr1); + items.add(arr2); + items.add(arr3); + mMenu.setmMenuItems(items); + +// new Handler().postDelayed(new Runnable() { +// @Override +// public void run() { +// List items = new ArrayList<>(); +// items.add(arr1); +// items.add(arr2); +// items.add(arr3); +// mMenu.setmMenuItems(items); +// +// } +// }, 1000); - } - }, 1000); mMenu.setIsDebug(false); mList=(ListView)findViewById(R.id.lv_list); data=getData(); mList.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, data)); - } private void setFilter(){ diff --git a/library/src/main/java/com/jayfang/dropdownmenu/DropDownMenu.java b/library/src/main/java/com/jayfang/dropdownmenu/DropDownMenu.java index de525bd..a62e335 100644 --- a/library/src/main/java/com/jayfang/dropdownmenu/DropDownMenu.java +++ b/library/src/main/java/com/jayfang/dropdownmenu/DropDownMenu.java @@ -28,10 +28,10 @@ public class DropDownMenu extends LinearLayout{ // Menu 展开的ListView 的 adapter - private List MenuAdapters=new ArrayList(); + private List mMenuAdapters=new ArrayList(); // Menu 展开的 list item - private List MenuItems=new ArrayList<>(); + private List mMenuItems=new ArrayList<>(); //菜单 上的文字 private List mTvMenuTitles=new ArrayList<>(); @@ -40,131 +40,137 @@ public class DropDownMenu extends LinearLayout{ //菜单 的箭头 private List mIvMenuArrow=new ArrayList<>(); - private Context context; + private Context mContext; private PopupWindow mPopupWindow; + // Menu 展开的ListView private ListView mMenuList; // Menu 展开的ListView 下部的阴影 private RelativeLayout mRlShadow; // 监听器 - private OnMenuSelectedListener MenuSelectedListener; + private OnMenuSelectedListener mMenuSelectedListener; // 主Menu的个数 - private int MenuCount; + private int mMenuCount; // Menu 展开的list 显示数量 - private int ShowCount; + private int mShowCount; //选中行数 - private int RowSelected=0; + private int mRowSelected=0; //选中列数 - private int ColumnSelected=0; + private int mColumnSelected=0; //Menu的字体颜色 - private int MenuTitleTextColor; + private int mMenuTitleTextColor; //Menu的字体大小 - private int MenuTitleTextSize; + private int mMenuTitleTextSize; //Menu的按下的字体颜色 - private int MenuPressedTitleTextColor; + private int mMenuPressedTitleTextColor; //Menu的按下背景 - private int MenuPressedBackColor; + private int mMenuPressedBackColor; //Menu的背景 - private int MenuBackColor; + private int mMenuBackColor; //Menu list 的字体大小 - private int MenuListTextSize; + private int mMenuListTextSize; //Menu list 的字体颜色 - private int MenuListTextColor; + private int mMenuListTextColor; //是否显示选中的对勾 - private boolean showCheck; + private boolean mShowCheck; //是否现实列表的分割线 - private boolean showDivider; + private boolean mShowDivider; //列表的背景 - private int MenuListBackColor; + private int mMenuListBackColor; //列表的按下效果 - private int MenuListSelectorRes; + private int mMenuListSelectorRes; //箭头距离 - private int ArrowMarginTitle; + private int mArrowMarginTitle; //对勾的图片资源 - private int CheckIcon; + private int mCheckIcon; //向上的箭头图片资源 - private int UpArrow; + private int mUpArrow; //向下的箭头图片资源 - private int DownArrow; + private int mDownArrow; - private boolean drawable=false; + private boolean mDrawable=false; - private String[] defaultMenuTitle; + private String[] mDefaultMenuTitle; private boolean isDebug=true; - public DropDownMenu(Context context) { - super(context); - init(context); + public DropDownMenu(Context mContext) { + super(mContext); + init(mContext); } - public DropDownMenu(Context context, AttributeSet attrs) { - super(context, attrs); - init(context); + public DropDownMenu(Context mContext, AttributeSet attrs) { + super(mContext, attrs); + init(mContext); } - private void init(Context context){ - this.context=context; - MenuCount=2; - ShowCount=5; - MenuTitleTextColor=getResources().getColor(R.color.default_menu_text); - MenuPressedBackColor=getResources().getColor(R.color.default_menu_press_back); - MenuPressedTitleTextColor=getResources().getColor(R.color.default_menu_press_text); - MenuBackColor=getResources().getColor(R.color.default_menu_back); - MenuListBackColor=getResources().getColor(R.color.white); - MenuListSelectorRes=R.color.white; - MenuTitleTextSize=18; - ArrowMarginTitle=10; - showCheck=true; - showDivider=true; - CheckIcon=R.drawable.ico_make; - UpArrow=R.drawable.arrow_up; - DownArrow=R.drawable.arrow_down; + private void init(Context mContext){ + this.mContext=mContext; + View popWindows = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_menu, null); + mPopupWindow = new PopupWindow(popWindows, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); + mMenuList = (ListView) popWindows.findViewById(R.id.lv_menu); + mRlShadow = (RelativeLayout) popWindows.findViewById(R.id.rl_menu_shadow); + + mMenuCount=2; + mShowCount=5; + mMenuTitleTextColor=getResources().getColor(R.color.default_menu_text); + mMenuPressedBackColor=getResources().getColor(R.color.default_menu_press_back); + mMenuPressedTitleTextColor=getResources().getColor(R.color.default_menu_press_text); + mMenuBackColor=getResources().getColor(R.color.default_menu_back); + mMenuListBackColor=getResources().getColor(R.color.white); + mMenuListSelectorRes=R.color.white; + mMenuTitleTextSize=18; + mArrowMarginTitle=10; + mShowCheck=true; + mShowDivider=true; + mCheckIcon=R.drawable.ico_make; + mUpArrow=R.drawable.arrow_up; + mDownArrow=R.drawable.arrow_down; } // 设置 Menu的item - public void setMenuItems(List menuItems) { - MenuItems = menuItems; - drawable=true; + public void setmMenuItems(List menuItems) { + mMenuItems = menuItems; + mDrawable=true; invalidate(); } // 设置 Menu的数量 - public void setMenuCount(int menuCount) { - MenuCount = menuCount; + public void setmMenuCount(int menuCount) { + mMenuCount = menuCount; } - public void setShowDivider(boolean showDivider) { - this.showDivider = showDivider; + public void setShowDivider(boolean mShowDivider) { + this.mShowDivider = mShowDivider; } - public void setMenuListBackColor(int menuListBackColor) { - MenuListBackColor = menuListBackColor; + public void setmMenuListBackColor(int menuListBackColor) { + mMenuListBackColor = menuListBackColor; } - public void setMenuListSelectorRes(int menuListSelectorRes) { - MenuListSelectorRes = menuListSelectorRes; + public void setmMenuListSelectorRes(int menuListSelectorRes) { + mMenuListSelectorRes = menuListSelectorRes; } - public void setArrowMarginTitle(int arrowMarginTitle) { - ArrowMarginTitle = arrowMarginTitle; + public void setmArrowMarginTitle(int arrowMarginTitle) { + mArrowMarginTitle = arrowMarginTitle; } - public void setMenuPressedTitleTextColor(int menuPressedTitleTextColor) { - MenuPressedTitleTextColor = menuPressedTitleTextColor; + public void setmMenuPressedTitleTextColor(int menuPressedTitleTextColor) { + mMenuPressedTitleTextColor = menuPressedTitleTextColor; } - public void setDefaultMenuTitle(String[] defaultMenuTitle) { - this.defaultMenuTitle = defaultMenuTitle; + public void setDefaultMenuTitle(String[] mDefaultMenuTitle) { + this.mDefaultMenuTitle = mDefaultMenuTitle; } public void setIsDebug(boolean isDebug) { @@ -172,190 +178,186 @@ public void setIsDebug(boolean isDebug) { } // 设置 show 数量 - public void setShowCount(int showCount) { - ShowCount = showCount; + public void setmShowCount(int showCount) { + mShowCount = showCount; } // 设置 Menu的字体颜色 - public void setMenuTitleTextColor(int menuTitleTextColor) { - MenuTitleTextColor = menuTitleTextColor; + public void setmMenuTitleTextColor(int menuTitleTextColor) { + mMenuTitleTextColor = menuTitleTextColor; } // 设置 Menu的字体大小 - public void setMenuTitleTextSize(int menuTitleTextSize) { - MenuTitleTextSize = menuTitleTextSize; + public void setmMenuTitleTextSize(int menuTitleTextSize) { + mMenuTitleTextSize = menuTitleTextSize; } //设置Menu的背景色 - public void setMenuBackColor(int menuBackColor) { - MenuBackColor = menuBackColor; + public void setmMenuBackColor(int menuBackColor) { + mMenuBackColor = menuBackColor; } //设置Menu的按下背景色 - public void setMenuPressedBackColor(int menuPressedBackColor) { - MenuPressedBackColor = menuPressedBackColor; + public void setmMenuPressedBackColor(int menuPressedBackColor) { + mMenuPressedBackColor = menuPressedBackColor; } //设置Menu list的字体颜色 - public void setMenuListTextColor(int menuListTextColor) { - MenuListTextColor = menuListTextColor; - for (int i=0;i parent, View view, int position, long id) { + mPopupWindow.dismiss(); + mRowSelected = position; + + mTvMenuTitles.get(mColumnSelected).setText(mMenuItems.get(mColumnSelected)[mRowSelected]); + mIvMenuArrow.get(mColumnSelected).setImageResource(mDownArrow); + mMenuAdapters.get(mColumnSelected).setSelectIndex(mRowSelected); + if (mMenuSelectedListener == null && isDebug) + Toast.makeText(mContext, "MenuSelectedListener is null", Toast.LENGTH_LONG).show(); + else + mMenuSelectedListener.onSelected(view, mRowSelected, mColumnSelected); + } + }); + + mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { + @Override + public void onDismiss() { + for (int i = 0; i < mMenuCount; i++) { + mIvMenuArrow.get(i).setImageResource(mDownArrow); + mRlMenuBacks.get(i).setBackgroundColor(mMenuBackColor); + mTvMenuTitles.get(i).setTextColor(mMenuTitleTextColor); } - }); + } + }); - mMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - mPopupWindow.dismiss(); - RowSelected = position; - - mTvMenuTitles.get(ColumnSelected).setText(MenuItems.get(ColumnSelected)[RowSelected]); - mIvMenuArrow.get(ColumnSelected).setImageResource(DownArrow); - MenuAdapters.get(ColumnSelected).setSelectIndex(RowSelected); - if (MenuSelectedListener == null && isDebug) - Toast.makeText(context, "MenuSelectedListener is null", Toast.LENGTH_LONG).show(); - else - MenuSelectedListener.onSelected(view, RowSelected, ColumnSelected); - } - }); + if (mMenuItems.size() != mMenuCount) { + if (isDebug) + Toast.makeText(mContext, "Menu item is not setted or incorrect", Toast.LENGTH_LONG).show(); + return; + } - mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { - @Override - public void onDismiss() { - for (int i = 0; i < MenuCount; i++) { - mIvMenuArrow.get(i).setImageResource(DownArrow); - mRlMenuBacks.get(i).setBackgroundColor(MenuBackColor); - mTvMenuTitles.get(i).setTextColor(MenuTitleTextColor); - } - } - }); + if (mMenuAdapters.size() == 0) { + for (int i = 0; i < mMenuCount; i++) { + MenuListAdapter adapter = new MenuListAdapter(mContext, mMenuItems.get(i)); + adapter.setShowCheck(mShowCheck); + adapter.setCheckIcon(mCheckIcon); + mMenuAdapters.add(adapter); - if (MenuItems.size() != MenuCount) { - if (isDebug) - Toast.makeText(context, "Menu item is not setted or incorrect", Toast.LENGTH_LONG).show(); - return; } + } else if (mMenuAdapters.size() != mMenuCount) { + if (isDebug) + Toast.makeText(mContext, "If you want set Adapter by yourself,please ensure the number of adpaters equal mMenuCount", Toast.LENGTH_LONG).show(); + return; + } + int width = getWidth(); + + for (int i = 0; i < mMenuCount; i++) { + final RelativeLayout v = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.menu_item, null, false); + 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); - if (MenuAdapters.size() == 0) { - for (int i = 0; i < MenuCount; i++) { - MenuListAdapter adapter = new MenuListAdapter(context, MenuItems.get(i)); - adapter.setShowCheck(showCheck); - adapter.setCheckIcon(CheckIcon); - MenuAdapters.add(adapter); + RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.rl_menu_head); + rl.setBackgroundColor(mMenuBackColor); + mRlMenuBacks.add(rl); - } - } else if (MenuAdapters.size() != MenuCount) { - if (isDebug) - Toast.makeText(context, "If you want set Adapter by yourself,please ensure the number of adpaters equal MenuCount", Toast.LENGTH_LONG).show(); - return; - } - int width = getWidth(); - - for (int i = 0; i < MenuCount; i++) { - final RelativeLayout v = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.menu_item, null, false); - RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(width / MenuCount, LayoutParams.WRAP_CONTENT); - v.setLayoutParams(parms); - TextView tv = (TextView) v.findViewById(R.id.tv_menu_title); - tv.setTextColor(MenuTitleTextColor); - tv.setTextSize(MenuTitleTextSize); - if (defaultMenuTitle==null||defaultMenuTitle.length==0){ - tv.setText(MenuItems.get(i)[0]); - }else{ - tv.setText(defaultMenuTitle[i]); - } - this.addView(v, i); - mTvMenuTitles.add(tv); - - RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.rl_menu_head); - rl.setBackgroundColor(MenuBackColor); - mRlMenuBacks.add(rl); - - ImageView iv = (ImageView) v.findViewById(R.id.iv_menu_arrow); - mIvMenuArrow.add(iv); - mIvMenuArrow.get(i).setImageResource(DownArrow); - - RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv.getLayoutParams(); - params.leftMargin = ArrowMarginTitle; - iv.setLayoutParams(params); - - final int index = i; - v.setOnClickListener(new OnClickListener() { - @Override - public void onClick(View view) { - mMenuList.setAdapter(MenuAdapters.get(index)); - if (MenuAdapters.get(index).getCount() > ShowCount) { - View childView = MenuAdapters.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() * ShowCount); - mMenuList.setLayoutParams(parms); - } else { - View childView = MenuAdapters.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 (!showDivider) - mMenuList.setDivider(null); - mMenuList.setBackgroundColor(MenuListBackColor); - mMenuList.setSelector(MenuListSelectorRes); - ColumnSelected = index; - mTvMenuTitles.get(index).setTextColor(MenuPressedTitleTextColor); - mRlMenuBacks.get(index).setBackgroundColor(MenuPressedBackColor); - mIvMenuArrow.get(index).setImageResource(UpArrow); - mPopupWindow.showAsDropDown(v); + ImageView iv = (ImageView) v.findViewById(R.id.iv_menu_arrow); + mIvMenuArrow.add(iv); + mIvMenuArrow.get(i).setImageResource(mDownArrow); + + 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) { + 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); } - }); - } - drawable=false; + if (!mShowDivider) + mMenuList.setDivider(null); + mMenuList.setBackgroundColor(mMenuListBackColor); + mMenuList.setSelector(mMenuListSelectorRes); + mColumnSelected = index; + mTvMenuTitles.get(index).setTextColor(mMenuPressedTitleTextColor); + mRlMenuBacks.get(index).setBackgroundColor(mMenuPressedBackColor); + mIvMenuArrow.get(index).setImageResource(mUpArrow); + mPopupWindow.showAsDropDown(v); + } + }); } + mDrawable=false; + } }