会经常用户到菜单选项提供给用户选择,例如选择图片,图库和相机选择等一系列场景吧,根据为了以后更加方便使用通过自定义封装了一个菜单,主要是通过一个列表展示,将菜单项列表传入设置参数就可以显示,使用方便简单只需要几行代码就可以,可以显示在底部,居中和某个控件的下方。先看效果图吧
居中显示
底部显示
置于控件下方
看过效果图接下来看一下用法
List menus =new ArrayList<>();
PopMenu popMenu=new PopMenu.MenuBuilder(activity,menus, new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
})//必须设置 当前activity,menus 菜单项,菜单点击事件
.focusable(true)//是否获取焦点
.outsideTouchable(true)//点击外部是否小时
.showIco(false)//是否显示菜单图标
.setElevation(true)//背景是否置灰
.hasDiver(false)//是否显示分割线
.setItemBackground(R.drawable.pop_menu_item_selector)//菜单子项背景
.show(rightImg,0,60);//置于控件下方
只需要设置需要的属性就可以显示并使用了,另外show()默认居中显示 showBottom()在底部显示
用法是不是很简单呢,接下来就要看源码了
package shens.library.base.view.menu;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import java.util.List;
import shens.library.base.R;
import shens.library.base.adapter.recyle.BaseQuickAdapter;
import shens.library.base.utils.ScreenUtils;
public class PopMenu {
private ActivitymContext;//上下文
private ViewmPopWindowView;
private RecyclerViewmenuListView;
private PopupWindowpopupWindow;
private PopupWindow.OnDismissListenerdimisListener;
private BaseQuickAdapter.OnItemClickListeneronItemClickListener;
private MenuListAdaptermenuListAdapter;
private ListmenuModels;
private boolean isShowIco =false;
private int mWidth;
private boolean isSetElevation=true;//背景是否阴影
public PopMenu(Activity mContext, List menus){
this.mContext = mContext;
popupWindow =new PopupWindow(mContext);
this.menuModels = menus;
initView();
}
private void initView() {
mWidth= ScreenUtils.getScreemWidth(mContext)/2;
mPopWindowView = LayoutInflater.from(mContext).inflate(R.layout.pop_menu_view,null);
menuListView = (RecyclerView)mPopWindowView.findViewById(R.id.pop_menu_listview);
menuListView.setLayoutManager(new LinearLayoutManager(mContext));
menuListAdapter=new MenuListAdapter(isShowIco,LinearLayoutManager.VERTICAL ,R.layout.pop_menu_item,menuModels);
menuListAdapter.setOnItemClickListener(onItemClickListener);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setWidth(mWidth);
popupWindow.setContentView(mPopWindowView);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setViewNoElevation();
if(dimisListener!=null){
dimisListener.onDismiss();
}
}
});
}
public void setOnItemClickListener(BaseQuickAdapter.OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
menuListAdapter.setOnItemClickListener(this.onItemClickListener);
}
public void setWidth(int width){
this.mWidth = width;
}
public void setHasDiver(boolean isHasDivier){
menuListAdapter.setIsDiver(isHasDivier);
}
public void setMenuItemBackground(int resid){
menuListAdapter.setItemBackground(resid);
}
public void showCenter(){
setViewElevation();
menuListView.setAdapter(menuListAdapter);
popupWindow.setAnimationStyle(R.style.popupwindow_alph);
popupWindow.showAtLocation(mContext.getWindow().getDecorView(), Gravity.CENTER,0,0);
}
public void show(){
showCenter();
}
public void showBottom(){
setViewElevation();
menuListView.setAdapter(menuListAdapter);
popupWindow.setAnimationStyle(R.style.popupwindow_botoom);
popupWindow.showAtLocation(mContext.getWindow().getDecorView(), Gravity.BOTTOM,0,0);
}
public void setIsshowIco(boolean isshowIco){
this.isShowIco=isshowIco;
}
public void setElevation(boolean isSetElevation){
this.isSetElevation=isSetElevation;
}
private void setFocusable(boolean focusable){
popupWindow.setFocusable(focusable);
}
private void setOutSideable(boolean outSideable){
popupWindow.setOutsideTouchable(outSideable);
}
private void setViewElevation(){
//产生背景变暗效果
if(isSetElevation){
WindowManager.LayoutParams lp =mContext.getWindow().getAttributes();
lp.alpha =0.6f;
mContext.getWindow().setAttributes(lp);
}
}
private void setViewNoElevation(){
if(isSetElevation){
WindowManager.LayoutParams lp =mContext.getWindow().getAttributes();
lp.alpha =1.0f;
mContext.getWindow().setAttributes(lp);
}
}
public void show(View view){
setViewElevation();
menuListView.setAdapter(menuListAdapter);
popupWindow.showAsDropDown(view);
}
public void show(View view,int x,int y){
setViewElevation();
menuListView.setAdapter(menuListAdapter);
popupWindow.showAsDropDown(view,x,y);
}
public void dismis(){
if(popupWindow!=null)
popupWindow.dismiss();
}
public MenuListAdaptergetAdapter(){
return menuListAdapter;
}
public void setOndismisListener(PopupWindow.OnDismissListener dimisListener){
this.dimisListener =dimisListener;
}
public static class MenuBuilder{
private Activityactivity;
Listmenus;
private PopupWindow.OnDismissListenerdimisListener;
private BaseQuickAdapter.OnItemClickListeneronItemClickListener;
private boolean isShowIco =false;//是否显示图标
private boolean isHasDiver=false;//是否显示分割线
private int itemBackResId;
private int mWidth;
private boolean isSetElevation=true;
private boolean isFocused =true;
private boolean isOutSideTouchable =true;
public MenuBuilder(Activity activity,List menus,BaseQuickAdapter.OnItemClickListener onItemClickListener){
this.activity =activity;
this.menus = menus;
this.onItemClickListener = onItemClickListener;
mWidth= ScreenUtils.getScreemWidth(activity)/2;//默认宽度为屏幕的一半
itemBackResId=R.drawable.pop_menu_item_selector;
}
public MenuBuilderdimisListener(PopupWindow.OnDismissListener dimisListener){
this.dimisListener = dimisListener;
return this;
}
public MenuBuildersetItemBackground(int resid){
this.itemBackResId = resid;
return this;
}
public MenuBuildershowIco(boolean isShowIco){
this.isShowIco = isShowIco;
return this;
}
public MenuBuilderhasDiver(boolean isHasDiver){
this.isHasDiver = isHasDiver;
return this;
}
public MenuBuilderfocusable(boolean isFocused){
this.isFocused = isFocused;
return this;
}
public MenuBuilderoutsideTouchable(boolean isOutSideTouchable){
this.isOutSideTouchable = isOutSideTouchable;
return this;
}
public MenuBuildersetElevation(boolean isSetElevation){
this.isSetElevation = isSetElevation;
return this;
}
public MenuBuilderwidth(int mWidth){
this.mWidth = mWidth;
return this;
}
public PopMenucreate(){
PopMenu popMenu =new PopMenu(activity,menus);
popMenu.setWidth(mWidth);
popMenu.setHasDiver(true);
popMenu.setIsshowIco(isShowIco);
popMenu.setElevation(isSetElevation);
popMenu.setFocusable(isFocused);
popMenu.setHasDiver(isHasDiver);
popMenu.setOutSideable(isOutSideTouchable);
popMenu.setMenuItemBackground(itemBackResId);
popMenu.setOndismisListener(dimisListener);
popMenu.setOnItemClickListener(onItemClickListener);
return popMenu;
}
public PopMenushow(){
PopMenu popMenu = create();
popMenu.show();
return popMenu;
}
public PopMenushowBottom(){
PopMenu popMenu = create();
popMenu.showBottom();
return popMenu;
}
public PopMenushow(View view,int x,int y){
PopMenu popMenu = create();
popMenu.show(view,x,y);
return popMenu;
}
}
}
以上就是菜单的核心代码了,需要详细或者是有疑问可以评论留言沟通
举报/反馈