弹出(或下拉)菜单,在APP中的应用是经常用到的。这里分享自定带图标弹出菜单。
一, 自定带图标弹出菜单代码
在项目中将弹出菜单做为了一个库,是应用PopupWindow,RecyclerView实现的,其中有三个Java类(myMenuItem,bs60MenuAdapter,bs60PopMenu),二个Layout文件(bs60_menuitem.xml,bs60_popmenu.xml)。
1), Java 类
myMenuItem类
package com.bidaeview.hbs60.lib_toprightmenu;
import android.graphics.Color;
public class myMenuItem {
public static int txtColor = 0;
private String id;
private int icon;
private String text;
private boolean colorsetblv = false;
private int color_MenuTitle;
private int color_Menuicon;
public myMenuItem() {}
public myMenuItem(String text) {
this.text = text;
color_MenuTitle = Color.BLACK;
color_Menuicon = Color.BLACK;
colorsetblv = false;
}
public myMenuItem(int iconId, String text,int colorIcon) {
this.icon = iconId;
this.text = text;
color_MenuTitle = Color.BLACK;
color_Menuicon = colorIcon;
colorsetblv = true;
}
public myMenuItem(int iconId, String text) {
this.icon = iconId;
this.text = text;
color_MenuTitle = Color.BLACK;
color_Menuicon = Color.BLACK;
colorsetblv = false;
}
public myMenuItem(String id, int iconId, String text) {
this.id = id;
this.icon = iconId;
this.text = text;
color_MenuTitle = Color.BLACK;
color_Menuicon = Color.BLACK;
colorsetblv = false;
}
public myMenuItem(String id, int iconId, String text,int colorTitle,int colorIcon) {
this.id = id;
this.icon = iconId;
this.text = text;
color_MenuTitle = colorTitle;
color_Menuicon = colorIcon;
colorsetblv = true;
}
public myMenuItem(String id, int iconId, String text,int colorIcon) {
this.id = id;
this.icon = iconId;
this.text = text;
color_Menuicon = colorIcon;
color_MenuTitle = Color.BLACK;
colorsetblv = true;
}
public String getId() {
return id;
}
public boolean getIconColorSetv() {
return colorsetblv;
}
public void setId(String id) {
this.id = id;
}
public int getIcon() {
return icon;
}
public int getTitleColor() {
return color_MenuTitle;
}
public int getIconColor() {
return color_Menuicon;
}
public void setIcon(int iconId) {
this.icon = iconId;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
bs60MenuAdapter类
package com.bidaeview.hbs60.lib_toprightmenu;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.LinearLayout;
import java.util.List;
/**
* Author:bs60 on 2019.11.07
*/
public class bs60MenuAdapter extends RecyclerView.Adapter<bs60MenuAdapter.bsViewHolder> {
private Context mContext;
private List<myMenuItem> menuItemList;
private boolean showIcon;
private bs60PopMenu mPopMenu;
private int LineHeight = 90;
private bs60PopMenu.OnMenuItemClickListener onMenuItemClickListener;
private bs60PopMenu.OnMenuItemClick3PListener onMenuItemClick3PListener;
public bs60MenuAdapter(Context context, bs60PopMenu cpopMenu, List<myMenuItem> menuItemList, boolean show,int linHeight) {
this.mContext = context;
this.mPopMenu = cpopMenu;
this.menuItemList = menuItemList;
this.showIcon = show;
this.LineHeight = linHeight;
}
public void setData(List<myMenuItem> data){
menuItemList = data;
notifyDataSetChanged();
}
public void setShowIcon(boolean showIcon) {
this.showIcon = showIcon;
notifyDataSetChanged();
}
@Override
public bsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.bs60_menuitem, parent, false);
LinearLayout cLlayout = (LinearLayout)view.findViewById(R.id.clineLayout);
android.view.ViewGroup.LayoutParams params = cLlayout.getLayoutParams();
params.height=LineHeight;
/** 2019.11.11 修改ICON 大小有效,不影响行宽 */
/** ImageView cimageView = (ImageView)view.findViewById(R.id.menu_item_icon);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20,20);
cimageView.setLayoutParams(params); */
return new bsViewHolder(view);
}
@Override
public void onBindViewHolder(bsViewHolder holder, int position) {
final myMenuItem menuItem = menuItemList.get(position);
if (showIcon){
holder.icon.setVisibility(View.VISIBLE);
int resId = menuItem.getIcon();
if(menuItem.getIconColorSetv()) {
holder.icon.setColorFilter(menuItem.getIconColor());
}
holder.icon.setImageResource(resId < 0 ? 0 : resId);
}else{
holder.icon.setVisibility(View.GONE);
}
holder.text.setTextColor(menuItem.getTitleColor());// menuItemList.get(myMenuItem.txtColor);
holder.text.setText(menuItem.getText());
final int pos = holder.getAdapterPosition();
holder.container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onMenuItemClickListener != null) {
mPopMenu.dismiss();
onMenuItemClickListener.onMenuItemClick(pos,menuItemList.get(pos).getText());
}else{
if (onMenuItemClick3PListener != null) {
mPopMenu.dismiss();
onMenuItemClick3PListener.onMenuItem3PClick(pos,menuItemList.get(pos).getId(), menuItemList.get(pos).getText());
}
}
}
});
}
@Override
public int getItemCount() {
return menuItemList == null ? 0 : menuItemList.size();
}
class bsViewHolder extends RecyclerView.ViewHolder{
ViewGroup container;
ImageView icon;
TextView text;
bsViewHolder(View itemView) {
super(itemView);
container = (ViewGroup) itemView;
icon = (ImageView) itemView.findViewById(R.id.menu_item_icon);
text = (TextView) itemView.findViewById(R.id.menu_item_text);
}
}
public void setOnMenuItemClickListener(bs60PopMenu.OnMenuItemClickListener listener){
this.onMenuItemClickListener = listener;
}
public void setOnMenuItemClick3PListener(bs60PopMenu.OnMenuItemClick3PListener listener){
this.onMenuItemClick3PListener = listener;
}
}
bs60PopMenu 类
package com.bidaeview.hbs60.lib_toprightmenu;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.Color;// 2019.11.07 bu bs60
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import java.util.ArrayList;
import java.util.List;
/**
* Author:bs60 on 2019.11.7
*/
public class bs60PopMenu {
private Activity mContext;
private PopupWindow mPopupWindow;
private RecyclerView mRecyclerView;
private View content;
private bs60MenuAdapter mAdapter;
private List<myMenuItem> menuItemList;
private static final int DEFAULT_HEIGHT = 480;
private int popHeight = DEFAULT_HEIGHT;
private int popWidth = RecyclerView.LayoutParams.WRAP_CONTENT;
private boolean showIcon = true;
private boolean dimBackground = false;
private boolean needAnimationStyle = false;
private static final int DEFAULT_ANIM_STYLE = R.style.GRADUAL_ANIM_STYLE;
private int animationStyle;
private float alpha = 0.75f;
private int lineHeight = 32;
private int lineSpace = 90;
private int ixoffv = 90;
private int menuBgColor = Color.WHITE;
public bs60PopMenu(Activity context) {
this.mContext = context;
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
lineSpace = (int)(lineHeight*dm.scaledDensity);
ixoffv = (int)(lineHeight*dm.scaledDensity);
init();
}
public bs60PopMenu(Activity context,int icLinHeight) {
this.mContext = context;
final DisplayMetrics dm = context.getResources().getDisplayMetrics();
if(icLinHeight > 0) lineHeight = icLinHeight;
lineSpace = (int)(lineHeight*dm.scaledDensity);
init();
}
private void init() {
content = LayoutInflater.from(mContext).inflate(R.layout.bs60_popmenu, null);
mRecyclerView = (RecyclerView) content.findViewById(R.id.pomenu_recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
mRecyclerView.setBackgroundColor(Color.GREEN);
menuItemList = new ArrayList<>();
mAdapter = new bs60MenuAdapter(mContext, this, menuItemList, true,lineSpace);
myMenuItem.txtColor = Color.BLACK;
}
public bs60PopMenu setMenuBgColor(int bgColor){
this.menuBgColor = bgColor;
mRecyclerView.setBackgroundColor(bgColor);
return this;
}
public bs60PopMenu setMenuTxtColor(int txtColor){
myMenuItem.txtColor = txtColor;
return this;
}
public bs60PopMenu setWidth(int width){
if (width <= 0 && width != RecyclerView.LayoutParams.MATCH_PARENT){
this.popWidth = RecyclerView.LayoutParams.WRAP_CONTENT;
}else {
this.popWidth = width;
}
return this;
}
/**
* 是否显示菜单图标
*/
public bs60PopMenu showIcon(boolean show){
this.showIcon = show;
return this;
}
/**
* 添加多个菜单
*/
public bs60PopMenu addMenuList(List<myMenuItem> list,int popwHeight){
menuItemList.addAll(list);
this.popHeight = list.size()*lineSpace;
if(popwHeight > lineSpace){
this.popHeight = popwHeight;
}
return this;
}
/**
* 是否让背景变暗
*/
public bs60PopMenu dimBackground(boolean b){
this.dimBackground = b;
return this;
}
/**
* 否是需要动画
*/
public bs60PopMenu needAnimationStyle(boolean need){
this.needAnimationStyle = need;
return this;
}
/**
* 设置动画
*/
public bs60PopMenu setAnimationStyle(int style){
this.animationStyle = style;
return this;
}
public bs60PopMenu setOnMenuItemClickListener(OnMenuItemClickListener listener){
mAdapter.setOnMenuItemClickListener(listener);
return this;
}
public bs60PopMenu setOnMenuItemClick3PListener(OnMenuItemClick3PListener listener){
mAdapter.setOnMenuItemClick3PListener(listener);
return this;
}
private PopupWindow getPopupWindow(){
mPopupWindow = new PopupWindow(mContext);
mPopupWindow.setContentView(content);
mPopupWindow.setHeight(popHeight);
mPopupWindow.setWidth(popWidth);
if (needAnimationStyle){
mPopupWindow.setAnimationStyle(animationStyle <= 0 ? DEFAULT_ANIM_STYLE : animationStyle);
}
mPopupWindow.setFocusable(true);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(menuBgColor));
mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
if (dimBackground) {
setBackgroundAlpha(alpha, 1f, 300);
}
}
});
mAdapter.setData(menuItemList);
mAdapter.setShowIcon(showIcon);
mRecyclerView.setAdapter(mAdapter);
return mPopupWindow;
}
public bs60PopMenu showAsDropDown(View anchor){
showAsDropDown(anchor, 0, 0);
return this;
}
public bs60PopMenu showAsDropDown(View anchor, int xoff, int yoff){
if (mPopupWindow == null){
getPopupWindow();
}
if (!mPopupWindow.isShowing()) {
if(showIcon) {
mPopupWindow.showAsDropDown(anchor, xoff, yoff);
}else{
mPopupWindow.showAsDropDown(anchor, xoff+ixoffv, yoff);;
}
if (dimBackground){
setBackgroundAlpha(1f, alpha, 240);
}
}
return this;
}
private void setBackgroundAlpha(float from, float to, int duration) {
final WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
ValueAnimator animator = ValueAnimator.ofFloat(from, to);
animator.setDuration(duration);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
lp.alpha = (float) animation.getAnimatedValue();
mContext.getWindow().setAttributes(lp);
}
});
animator.start();
}
public void dismiss(){
if (mPopupWindow != null && mPopupWindow.isShowing()){
mPopupWindow.dismiss();
}
}
public interface OnMenuItemClickListener{
void onMenuItemClick(int position,String menuName);
}
public interface OnMenuItemClick3PListener{
void onMenuItem3PClick(int position,String menuID,String menuName);
}
}
2), Layout 文件
bs60_menuitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/clineLayout"
android:layout_width="match_parent"
android:layout_height="36dp"
android:gravity="center_vertical"
android:padding="3dp">
<ImageView
android:id="@+id/menu_item_icon"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginRight="8dp"
android:scaleType="center"/>
<TextView
android:id="@+id/menu_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:textSize="15sp"/>
</LinearLayout>
bs60_popmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pomenu_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
二,实例应用代码
private void MainFrameRightMenuSet() {
private bs60PopMenu mPopMenu = new bs60PopMenu(MainActivity.this);
List<myMenuItem> menuItems = new ArrayList<>();
menuItems.add(new myMenuItem("111", R.mipmap.calendar, getResources().getString(R.string.mUImenu_calendarFestival)));// "设置节假日历"));
menuItems.add(new myMenuItem("222", R.mipmap.calendar, getResources().getString(R.string.mUImenu_calendarWork)));//"设置加班日历"));
String floatTaskbutname = getResources().getString(R.string.mUImenu_onTimetaskset);// 定时任务参数设置
menuItems.add(new myMenuItem("333", R.mipmap.evenmenu_task, floatTaskbutname));
int icolor_darkGreen = getResources().getColor(R.color.pmenucolor_darkGreen);
menuItems.add(new myMenuItem("800", R.drawable.app_updateicon, getResources().getString(R.string.mUImenu_update),icolor_darkGreen));// 更新与升级
menuItems.add(new myMenuItem("888",R.drawable.comvec_clear, getResources().getString(R.string.mUImenu_exitApp)));//"退出APP"));
int iheight = 96*7;
int ibgcolorv = 0xCCFFFFFF;
int itxtcolorv = R.color.pmenucolor_black;
int ixMovOffv = 80;// Constants.calculate_MoveOffWidth(menuItems);
mPopMenu
.setMenuBgColor(ibgcolorv)
.setMenuTxtColor(itxtcolorv)
.setWidth(0) //默认宽度wrap_content
.showIcon(showIcon) //显示菜单图标,默认为true
.dimBackground(dimBg) //背景变暗,默认为true
.needAnimationStyle(needAnim) //显示动画,默认为true
.addMenuList(menuItems,0)
.setOnMenuItemClick3PListener(new bs60PopMenu.OnMenuItemClick3PListener() {
@Override
public void onMenuItem3PClick(int position,String menuID,String menuName) {
if(menuID.equals("111")){// 设置节假日历
FRAGMENTManager.beginTransaction().replace(R.id.content_frame, mFragmentStart).commit();
Message msg = new Message();
msg.what = SYSSET_CALENDARFESTIVAL;
msg.obj = "data";
handler.sendMessage(msg);
}
if(menuID.equals("222")){// 设置加班日历
FRAGMENTManager.beginTransaction().replace(R.id.content_frame, mFragmentStart).commit();
Message msg = new Message();
msg.what = SYSSET_CALENDARWORK;
msg.obj = "data";
handler.sendMessage(msg);
}
if(menuID.equals("333")){// < 任务 >浮动按钮
FRAGMENTManager.beginTransaction().replace(R.id.content_frame, mFragmentStart).commit();
Message msg = new Message();
msg.what = SYSSET_ONTIMETASKSET;
msg.obj = "data";
handler.sendMessage(msg);
}
}
})
.showAsDropDown(topRmenuBtn, -ixMovOffv, 0);
};