Browse Source

0.0.50.beta13 pre
- BottomMenu/PopMenu 新增 `.enableMenu(int[])`、`.enableMenu(CharSequence[])`、.enableMenu(String[]) 以及 `.disableMenu(int[])`、`.disableMenu(CharSequence[])`、.disableMenu(String[]) 用于将控制菜单是否可用,不可用的菜单将以 40% 透明度显示且点击无效;额外的增加了 `.isMenuItemEnable(int)` 用于判段指定索引的菜单是否可用;

Kongzue 10 months ago
parent
commit
8c9e1278b0

+ 78 - 5
DialogX/src/main/java/com/kongzue/dialogx/dialogs/BottomMenu.java

@@ -41,6 +41,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author: Kongzue
@@ -52,9 +53,7 @@ import java.util.List;
 public class BottomMenu extends BottomDialog {
 
     public enum SELECT_MODE {
-        NONE,
-        SINGLE,
-        MULTIPLE
+        NONE, SINGLE, MULTIPLE
     }
 
     protected BottomMenu me = this;
@@ -63,6 +62,7 @@ public class BottomMenu extends BottomDialog {
     protected ArrayList<Integer> selectionItems;
     protected boolean showSelectedBackgroundTips = false;
     protected MenuItemLayoutRefreshCallback<BottomMenu> menuMenuItemLayoutRefreshCallback;
+    protected Map<Integer, Boolean> menuUsability = new HashMap<Integer, Boolean>();
 
     protected OnMenuItemClickListener<BottomMenu> onMenuItemClickListener;
 
@@ -529,6 +529,9 @@ public class BottomMenu extends BottomDialog {
             listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                    if (!isMenuItemEnable(position)) {
+                        return;
+                    }
                     haptic(view);
                     long currentTime = System.currentTimeMillis();
                     if (currentTime - lastClickTime > ITEM_CLICK_DELAY) {
@@ -1452,7 +1455,7 @@ public class BottomMenu extends BottomDialog {
         return this;
     }
 
-    public BottomMenu appendMessage(CharSequence message){
+    public BottomMenu appendMessage(CharSequence message) {
         this.message = TextUtils.concat(this.message, message);
         refreshUI();
         return this;
@@ -1464,7 +1467,7 @@ public class BottomMenu extends BottomDialog {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                 getDialogView().setTranslationZ(orderIndex);
             } else {
-                error("DialogX: " + dialogKey() + " 执行 .setThisOrderIndex("+orderIndex+") 失败:系统不支持此方法,SDK-API 版本必须大于 21(LOLLIPOP)");
+                error("DialogX: " + dialogKey() + " 执行 .setThisOrderIndex(" + orderIndex + ") 失败:系统不支持此方法,SDK-API 版本必须大于 21(LOLLIPOP)");
             }
         }
         return this;
@@ -1474,4 +1477,74 @@ public class BottomMenu extends BottomDialog {
         setThisOrderIndex(getHighestOrderIndex());
         return this;
     }
+
+    public BottomMenu enableMenu(int... menuIndex) {
+        for (int i : menuIndex) {
+            menuUsability.put(i, true);
+        }
+        return this;
+    }
+
+    public BottomMenu enableMenu(CharSequence... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (CharSequence c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, true);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .enableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public BottomMenu enableMenu(String... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (String c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, true);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .enableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public BottomMenu disableMenu(int... menuIndex) {
+        for (int i : menuIndex) {
+            menuUsability.put(i, false);
+        }
+        return this;
+    }
+
+    public BottomMenu disableMenu(CharSequence... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (CharSequence c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, false);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .disableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public BottomMenu disableMenu(String... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (String c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, false);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .disableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public boolean isMenuItemEnable(int index) {
+        Boolean enabled = menuUsability.get(index);
+        if (enabled == null) {
+            return true;
+        }
+        return enabled;
+    }
 }

+ 75 - 0
DialogX/src/main/java/com/kongzue/dialogx/dialogs/PopMenu.java

@@ -51,6 +51,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @author: Kongzue
@@ -86,6 +87,7 @@ public class PopMenu extends BaseDialog {
     protected OnBackPressedListener<PopMenu> onBackPressedListener;
     protected MenuItemLayoutRefreshCallback<PopMenu> menuMenuItemLayoutRefreshCallback;
     protected int pressedIndex = -1;
+    protected Map<Integer, Boolean> menuUsability = new HashMap<Integer, Boolean>();
 
     protected int alignGravity = -1;                                        // 指定菜单相对 baseView 的位置
 
@@ -556,6 +558,9 @@ public class PopMenu extends BaseDialog {
             listMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+                    if (!isMenuItemEnable(position)) {
+                        return;
+                    }
                     haptic(view);
                     selectIndex = position;
                     if (!closing) {
@@ -1433,4 +1438,74 @@ public class PopMenu extends BaseDialog {
         setThisOrderIndex(getHighestOrderIndex());
         return this;
     }
+
+    public PopMenu enableMenu(int... menuIndex) {
+        for (int i : menuIndex) {
+            menuUsability.put(i, true);
+        }
+        return this;
+    }
+
+    public PopMenu enableMenu(CharSequence... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (CharSequence c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, true);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .enableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public PopMenu enableMenu(String... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (String c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, true);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .enableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public PopMenu disableMenu(int... menuIndex) {
+        for (int i : menuIndex) {
+            menuUsability.put(i, false);
+        }
+        return this;
+    }
+
+    public PopMenu disableMenu(CharSequence... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (CharSequence c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, false);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .disableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public PopMenu disableMenu(String... menuText) {
+        if (menuList != null && !menuList.isEmpty()) {
+            for (String c : menuText) {
+                int index = menuList.indexOf(c);
+                menuUsability.put(index, false);
+            }
+        } else {
+            error("DialogX: " + dialogKey() + " .disableMenu(" + menuText + ")执行失败,请先初始化菜单项 menuList");
+        }
+        return this;
+    }
+
+    public boolean isMenuItemEnable(int index) {
+        Boolean enabled = menuUsability.get(index);
+        if (enabled == null) {
+            return true;
+        }
+        return enabled;
+    }
 }

+ 3 - 2
DialogX/src/main/java/com/kongzue/dialogx/dialogs/WaitDialog.java

@@ -328,7 +328,7 @@ public class WaitDialog extends BaseDialog {
         }
 
         public void init() {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && getDialogView() != null) {
                 getDialogView().setTranslationZ(getThisOrderIndex());
             }
 
@@ -1225,13 +1225,14 @@ public class WaitDialog extends BaseDialog {
         refreshUI();
         return this;
     }
+
     public WaitDialog setThisOrderIndex(int orderIndex) {
         this.thisOrderIndex = orderIndex;
         if (getDialogView() != null) {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                 getDialogView().setTranslationZ(orderIndex);
             } else {
-                error("DialogX: " + dialogKey() + " 执行 .setThisOrderIndex("+orderIndex+") 失败:系统不支持此方法,SDK-API 版本必须大于 21(LOLLIPOP)");
+                error("DialogX: " + dialogKey() + " 执行 .setThisOrderIndex(" + orderIndex + ") 失败:系统不支持此方法,SDK-API 版本必须大于 21(LOLLIPOP)");
             }
         }
         return this;

+ 5 - 0
DialogX/src/main/java/com/kongzue/dialogx/util/BottomMenuArrayAdapter.java

@@ -95,6 +95,11 @@ public class BottomMenuArrayAdapter extends BaseAdapter {
         } else {
             viewHolder = (ViewHolder) convertView.getTag();
         }
+        if (!bottomMenu.isMenuItemEnable(position)) {
+            convertView.setAlpha(0.4f);
+        } else {
+            convertView.setAlpha(1f);
+        }
         if (bottomMenu.getSelectMode() == BottomMenu.SELECT_MODE.SINGLE) {
             if (viewHolder.imgDialogxMenuSelection != null) {
                 if (bottomMenu.getSelection() == position) {

+ 5 - 0
DialogX/src/main/java/com/kongzue/dialogx/util/PopMenuArrayAdapter.java

@@ -88,6 +88,11 @@ public class PopMenuArrayAdapter extends BaseAdapter {
         } else {
             viewHolder = (ViewHolder) convertView.getTag();
         }
+        if (!popMenu.isMenuItemEnable(position)) {
+            convertView.setAlpha(0.4f);
+        } else {
+            convertView.setAlpha(1f);
+        }
         int customBackgroundRes = popMenu.getStyle().popMenuSettings() == null ? 0 : popMenu.getStyle().popMenuSettings().overrideMenuItemBackgroundRes(popMenu.isLightTheme(), position, getCount(), false);
         if (customBackgroundRes != 0) {
             convertView.setBackgroundResource(customBackgroundRes);

+ 1 - 0
app/src/main/java/com/kongzue/dialogxdemo/activity/MainActivity.java

@@ -438,6 +438,7 @@ public class MainActivity extends BaseActivity {
             @Override
             public void onClick(View v) {
                 PopMenu.show("添加", "编辑", "删除", "分享")
+                        .disableMenu("编辑", "删除")
                         .setIconResIds(R.mipmap.img_dialogx_demo_add, R.mipmap.img_dialogx_demo_edit, R.mipmap.img_dialogx_demo_delete, R.mipmap.img_dialogx_demo_share)
                         .setOnMenuItemClickListener(new OnMenuItemClickListener<PopMenu>() {
                             @Override

+ 1 - 1
gradle.properties

@@ -19,7 +19,7 @@ android.useAndroidX=true
 # Automatically convert third-party libraries to use AndroidX
 android.enableJetifier=true
 
-BUILD_VERSION=0.0.50.beta12
+BUILD_VERSION=0.0.50.beta13
 BUILD_VERSION_INT=49
 DIALOGX_STYLE_VERSION=5
 android.nonTransitiveRClass=true