會經常用戶到菜單選項提供給用戶選擇,例如選擇圖片,圖庫和相機選擇等一系列場景吧,根據為了以后更加方便使用通過自定義封裝了一個菜單,主要是通過一個列表展示,將菜單項列表傳入設置參數就可以顯示,使用方便簡單只需要幾行代碼就可以,可以顯示在底部,居中和某個控件的下方。先看效果圖吧
看過效果圖接下來看一下用法
List<MenuModel>?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;
/**
* 彈出菜單
* Created by shenshilei on 2018/1/4.
* email ssl_java@163.com
* site http://www.houziyou.com
*/
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;
? ? ? ? }
}
}
以上就是菜單的核心代碼了,需要詳細或者是有疑問可以評論留言溝通