自定義通過PopupWindow實現通用選單

weixin_34214500發表於2018-01-09

會經常使用者到選單選項提供給使用者選擇,例如選擇圖片,相簿和相機選擇等一系列場景吧,根據為了以後更加方便使用通過自定義封裝了一個選單,主要是通過一個列表展示,將選單項列表傳入設定引數就可以顯示,使用方便簡單隻需要幾行程式碼就可以,可以顯示在底部,居中和某個控制元件的下方。先看效果圖吧


9377498-98fb341bba29f7d0.png
居中顯示
9377498-1ff826072a016738.png
底部顯示
9377498-b5b27ad20912e33e.png
置於控制元件下方

看過效果圖接下來看一下用法

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;

        }

}

}

以上就是選單的核心程式碼了,需要詳細或者是有疑問可以評論留言溝通

相關文章