自定義通過PopupWindow實現通用選單
會經常使用者到選單選項提供給使用者選擇,例如選擇圖片,相簿和相機選擇等一系列場景吧,根據為了以後更加方便使用通過自定義封裝了一個選單,主要是通過一個列表展示,將選單項列表傳入設定引數就可以顯示,使用方便簡單隻需要幾行程式碼就可以,可以顯示在底部,居中和某個控制元件的下方。先看效果圖吧
看過效果圖接下來看一下用法
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;
}
}
}
以上就是選單的核心程式碼了,需要詳細或者是有疑問可以評論留言溝通
相關文章
- 自定義右鍵選單實現過程詳解
- 自定義的PopupWindow
- 自定義View:側滑選單實現View
- 自定義View:側滑選單動畫實現View動畫
- 原生 JavaScript 如何實現通過自定義屬性定位/篩選節點JavaScript
- 通過xml檔案實現自定義圓角按鈕,以及點選效果XML
- Flutter 通過Clipper實現各種自定義形狀Flutter
- Feign通過自定義註解實現路徑的轉義
- django自定義構建模板,透過bootstrap實現選單隱藏和顯示Djangoboot
- 自定義ViewGroup,實現Android的側滑選單ViewAndroid
- Flutter實現自定義篩選框Flutter
- Spring Cloud Gateway 實現簡單自定義過濾器SpringCloudGateway過濾器
- 自定義簡單彈幕實現
- UIWebView自定義選單欄UIWebView
- 自定義右鍵選單
- 聊聊如何通過自定義註解實現springmvc和sentinel整合SpringMVC
- vue原生指令v-model實現自定義樣式の多選與單選Vue
- Vue.js自定義下拉選單,如何實現在下拉選單區域外點選即可關閉下拉選單的功能Vue.js
- 通用 PopupWindow,幾行程式碼搞定 PopupWindow 彈窗(續)行程
- 自定義View:自定義屬性(自定義按鈕實現)View
- 自定義 Windows 右鍵選單項Windows
- 自定義下拉選單控制元件控制元件
- Vue3.0實現原生高度可自定義選單元件vue3-menusVue元件
- 如何通過XIB 建立自定義UIViewUIView
- Java自定義Annotation,通過反射解析AnnotationJava反射
- 如何利用 Netty 實現自定義協議通訊?Netty協議
- android 螢幕適配一:通過自定義View的方式實現適配AndroidView
- vue-video-player,通過自定義按鈕元件實現全屏切換效果VueIDE元件
- Android Studio通過style和layer-list實現自定義進度條Android
- flutter-簡單實現找妹子自定義viewFlutterView
- IM即時通訊專案講解(二) 自定義實現圖片選擇GalleryViewView
- 封裝一個通用的PopupWindow封裝
- jQuery Validate自定義表單元素驗證通過和不通過的樣式jQuery
- 【Android初級】如何實現一個有動畫效果的自定義下拉選單Android動畫
- 基於JQuery的自定義樹形選單表格,實現展開、收起效果jQuery
- js自定義select下拉選單效果JS
- JavaScript自定義右鍵選單詳解JavaScript
- javascript自定義右鍵選單程式碼JavaScript