1
0
Эх сурвалжийг харах

0.0.50.beta8 ready
- 增加 BottomMenu 以及 PopMenu 的构建方法,现可通过类似于 `.show("添加", "编辑", "删除", "分享")` 的方法直接构建菜单;
- 增加 BottomMenu 以及 PopMenu 的图标设置方法,现可通过 `.setIconResIds(R.mipmap.img_dialogx_demo_add, R.mipmap.img_dialogx_demo_edit...)` 直接指定图标;
- 增加 BottomMenu 以及 PopMenu 设置图标是否根据亮暗色染色的方法,现可通过 `.setAutoTintIconInLightOrDarkMode(boolean)` 指定是否染色;
- 增加 BottomMenu 以及 PopMenu 设置菜单的方法,现可通过 `.setMenus("添加", "编辑", "删除", "分享")` 的方法直接设置菜单项;

Kongzue 1 жил өмнө
parent
commit
a4f0ae8dad

+ 60 - 6
DialogX/src/main/java/com/kongzue/dialogx/dialogs/BottomMenu.java

@@ -80,11 +80,13 @@ public class BottomMenu extends BottomDialog {
         super();
     }
 
-    private OnIconChangeCallBack<BottomMenu> onIconChangeCallBack;
-    private MenuItemTextInfoInterceptor<BottomMenu> menuItemTextInfoInterceptor;
-    private BottomDialogListView listView;
-    private BaseAdapter menuListAdapter;
-    private List<CharSequence> menuList;
+    protected OnIconChangeCallBack<BottomMenu> onIconChangeCallBack;
+    protected MenuItemTextInfoInterceptor<BottomMenu> menuItemTextInfoInterceptor;
+    protected BottomDialogListView listView;
+    protected BaseAdapter menuListAdapter;
+    protected List<CharSequence> menuList;
+    protected List<Integer> iconResIds;
+    protected boolean autoTintIconInLightOrDarkMode = true;
 
     public static BottomMenu show(List<CharSequence> menuList) {
         BottomMenu bottomMenu = new BottomMenu();
@@ -116,7 +118,7 @@ public class BottomMenu extends BottomDialog {
         return bottomMenu;
     }
 
-    public static BottomMenu show(String[] menuList) {
+    public static BottomMenu show(String... menuList) {
         BottomMenu bottomMenu = new BottomMenu();
         bottomMenu.setMenuList(menuList);
         bottomMenu.show();
@@ -707,6 +709,20 @@ public class BottomMenu extends BottomDialog {
         return this;
     }
 
+    public BottomMenu setMenus(CharSequence... menuList) {
+        this.menuList = Arrays.asList(menuList);
+        this.menuListAdapter = null;
+        preRefreshUI();
+        return this;
+    }
+
+    public BottomMenu setMenus(String... menuList) {
+        this.menuList = Arrays.asList(menuList);
+        this.menuListAdapter = null;
+        preRefreshUI();
+        return this;
+    }
+
     public OnIconChangeCallBack<BottomMenu> getOnIconChangeCallBack() {
         return onIconChangeCallBack;
     }
@@ -1390,4 +1406,42 @@ public class BottomMenu extends BottomDialog {
         refreshUI();
         return this;
     }
+
+    public List<Integer> getIconResIds() {
+        return iconResIds;
+    }
+
+    public int getIconResIds(int position) {
+        if (iconResIds != null && position >= 0 && position < iconResIds.size()) {
+            return iconResIds.get(position);
+        }
+        return 0;
+    }
+
+    public BottomMenu setIconResIds(List<Integer> iconResIds) {
+        this.iconResIds = iconResIds;
+        refreshUI();
+        return this;
+    }
+
+    public BottomMenu setIconResIds(int... resIds) {
+        if (iconResIds == null) {
+            iconResIds = new ArrayList<>();
+        }
+        for (int id : resIds) {
+            iconResIds.add(id);
+        }
+        refreshUI();
+        return this;
+    }
+
+    public boolean isAutoTintIconInLightOrDarkMode() {
+        return autoTintIconInLightOrDarkMode;
+    }
+
+    public BottomMenu setAutoTintIconInLightOrDarkMode(boolean autoTintIconInLightOrDarkMode) {
+        this.autoTintIconInLightOrDarkMode = autoTintIconInLightOrDarkMode;
+        refreshUI();
+        return this;
+    }
 }

+ 66 - 2
DialogX/src/main/java/com/kongzue/dialogx/dialogs/PopMenu.java

@@ -72,6 +72,8 @@ public class PopMenu extends BaseDialog {
     protected DialogLifecycleCallback<PopMenu> dialogLifecycleCallback;     //对话框生命周期
     protected OnBackgroundMaskClickListener<PopMenu> onBackgroundMaskClickListener;
     protected List<CharSequence> menuList;
+    protected List<Integer> iconResIds;
+    protected boolean autoTintIconInLightOrDarkMode = true;
     protected DialogImpl dialogImpl;
     protected WeakReference<View> baseViewWeakReference;
     protected boolean overlayBaseView = true;                               //允许菜单覆盖在 baseView 上
@@ -114,7 +116,12 @@ public class PopMenu extends BaseDialog {
         this.menuList.addAll(menuList);
     }
 
-    public PopMenu(CharSequence[] menuList) {
+    public PopMenu(CharSequence... menuList) {
+        this.menuList = new ArrayList<>();
+        this.menuList.addAll(Arrays.asList(menuList));
+    }
+
+    public PopMenu(String... menuList) {
         this.menuList = new ArrayList<>();
         this.menuList.addAll(Arrays.asList(menuList));
     }
@@ -162,7 +169,13 @@ public class PopMenu extends BaseDialog {
         return new PopMenu().setStyle(style);
     }
 
-    public static PopMenu show(CharSequence[] menus) {
+    public static PopMenu show(CharSequence... menus) {
+        PopMenu popMenu = new PopMenu(menus);
+        popMenu.show();
+        return popMenu;
+    }
+
+    public static PopMenu show(String... menus) {
         PopMenu popMenu = new PopMenu(menus);
         popMenu.show();
         return popMenu;
@@ -945,6 +958,20 @@ public class PopMenu extends BaseDialog {
         return this;
     }
 
+    public PopMenu setMenus(String... menuList) {
+        this.menuList = new ArrayList<>();
+        this.menuList.addAll(Arrays.asList(menuList));
+        refreshUI();
+        return this;
+    }
+
+    public PopMenu setMenus(CharSequence... menuList) {
+        this.menuList = new ArrayList<>();
+        this.menuList.addAll(Arrays.asList(menuList));
+        refreshUI();
+        return this;
+    }
+
     public void refreshUI() {
         if (getDialogImpl() == null) {
             return;
@@ -1334,4 +1361,41 @@ public class PopMenu extends BaseDialog {
         refreshUI();
         return this;
     }
+
+    public List<Integer> getIconResIds() {
+        return iconResIds;
+    }
+
+    public int getIconResIds(int position) {
+        if (iconResIds != null && position >= 0 && position < iconResIds.size()) {
+            return iconResIds.get(position);
+        }
+        return 0;
+    }
+
+    public PopMenu setIconResIds(int... resIds) {
+        if (iconResIds == null) {
+            iconResIds = new ArrayList<>();
+        }
+        for (int id : resIds) {
+            iconResIds.add(id);
+        }
+        refreshUI();
+        return this;
+    }
+
+    public PopMenu setIconResIds(List<Integer> iconResIds) {
+        this.iconResIds = iconResIds;
+        refreshUI();
+        return this;
+    }
+
+    public boolean isAutoTintIconInLightOrDarkMode() {
+        return autoTintIconInLightOrDarkMode;
+    }
+
+    public PopMenu setAutoTintIconInLightOrDarkMode(boolean autoTintIconInLightOrDarkMode) {
+        this.autoTintIconInLightOrDarkMode = autoTintIconInLightOrDarkMode;
+        return this;
+    }
 }

+ 2 - 2
DialogX/src/main/java/com/kongzue/dialogx/interfaces/OnIconChangeCallBack.java

@@ -11,7 +11,7 @@ import com.kongzue.dialogx.dialogs.BottomMenu;
  */
 public abstract class OnIconChangeCallBack<D extends BaseDialog> {
     
-    private boolean autoTintIconInLightOrDarkMode;
+    private Boolean autoTintIconInLightOrDarkMode;
     
     public OnIconChangeCallBack() {
     }
@@ -22,7 +22,7 @@ public abstract class OnIconChangeCallBack<D extends BaseDialog> {
     
     public abstract int getIcon(D dialog, int index, String menuText);
     
-    public boolean isAutoTintIconInLightOrDarkMode() {
+    public Boolean isAutoTintIconInLightOrDarkMode() {
         return autoTintIconInLightOrDarkMode;
     }
 }

+ 43 - 20
DialogX/src/main/java/com/kongzue/dialogx/util/BottomMenuArrayAdapter.java

@@ -28,48 +28,48 @@ import java.util.List;
  * @createTime: 2020/10/7 0:00
  */
 public class BottomMenuArrayAdapter extends BaseAdapter {
-    
+
     private BottomMenu bottomMenu;
     public List<CharSequence> objects;
     public Context context;
-    
+
     public BottomMenuArrayAdapter(BottomMenu bottomMenu, Context context, List<CharSequence> objects) {
         this.objects = objects;
         this.context = context;
         this.bottomMenu = bottomMenu;
     }
-    
+
     class ViewHolder {
         ImageView imgDialogxMenuIcon;
         ImageView imgDialogxMenuSelection;
         TextView txtDialogxMenuText;
         Space spaceDialogxRightPadding;
     }
-    
+
     @Override
     public int getCount() {
         return objects.size();
     }
-    
+
     @Override
     public CharSequence getItem(int position) {
         return objects.get(position);
     }
-    
+
     @Override
     public long getItemId(int position) {
         return position;
     }
-    
+
     TextInfo defaultMenuTextInfo;
-    
+
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
         ViewHolder viewHolder = null;
         if (convertView == null) {
             viewHolder = new ViewHolder();
             LayoutInflater mInflater = LayoutInflater.from(context);
-            
+
             int resourceId = R.layout.item_dialogx_material_bottom_menu_normal_text;
             if (bottomMenu.getStyle().overrideBottomDialogRes() != null) {
                 resourceId = bottomMenu.getStyle().overrideBottomDialogRes().overrideMenuItemLayout(bottomMenu.isLightTheme(), position, getCount(), false);
@@ -85,12 +85,12 @@ public class BottomMenuArrayAdapter extends BaseAdapter {
                 }
             }
             convertView = mInflater.inflate(resourceId, null);
-            
+
             viewHolder.imgDialogxMenuIcon = convertView.findViewById(R.id.img_dialogx_menu_icon);
             viewHolder.imgDialogxMenuSelection = convertView.findViewById(R.id.img_dialogx_menu_selection);
             viewHolder.txtDialogxMenuText = convertView.findViewById(R.id.txt_dialogx_menu_text);
             viewHolder.spaceDialogxRightPadding = convertView.findViewById(R.id.space_dialogx_right_padding);
-            
+
             convertView.setTag(viewHolder);
         } else {
             viewHolder = (ViewHolder) convertView.getTag();
@@ -147,14 +147,14 @@ public class BottomMenuArrayAdapter extends BaseAdapter {
             }
         }
         CharSequence text = objects.get(position);
-        
+
         int textColor = bottomMenu.isLightTheme() ? R.color.black90 : R.color.white90;
         if (bottomMenu.getStyle().overrideBottomDialogRes() != null) {
             if (bottomMenu.getStyle().overrideBottomDialogRes().overrideMenuTextColor(bottomMenu.isLightTheme()) != 0) {
                 textColor = bottomMenu.getStyle().overrideBottomDialogRes().overrideMenuTextColor(bottomMenu.isLightTheme());
             }
         }
-        
+
         if (null != text) {
             if (defaultMenuTextInfo == null) {
                 defaultMenuTextInfo = new TextInfo()
@@ -192,11 +192,11 @@ public class BottomMenuArrayAdapter extends BaseAdapter {
                     }
                 }
             }
-            
+
             if (bottomMenu.getOnIconChangeCallBack() != null) {
                 int resId = bottomMenu.getOnIconChangeCallBack().getIcon(bottomMenu, position, text.toString());
-                boolean autoTintIconInLightOrDarkMode = bottomMenu.getOnIconChangeCallBack().isAutoTintIconInLightOrDarkMode();
-                
+                boolean autoTintIconInLightOrDarkMode = bottomMenu.getOnIconChangeCallBack().isAutoTintIconInLightOrDarkMode() == null ? bottomMenu.isAutoTintIconInLightOrDarkMode() : bottomMenu.getOnIconChangeCallBack().isAutoTintIconInLightOrDarkMode();
+
                 if (resId != 0) {
                     viewHolder.imgDialogxMenuIcon.setVisibility(View.VISIBLE);
                     viewHolder.imgDialogxMenuIcon.setImageResource(resId);
@@ -215,9 +215,32 @@ public class BottomMenuArrayAdapter extends BaseAdapter {
                     }
                 }
             } else {
-                viewHolder.imgDialogxMenuIcon.setVisibility(View.GONE);
-                if (viewHolder.spaceDialogxRightPadding != null) {
-                    viewHolder.spaceDialogxRightPadding.setVisibility(View.GONE);
+                if (bottomMenu.getIconResIds() != null) {
+                    int resId = bottomMenu.getIconResIds(position);
+                    boolean autoTintIconInLightOrDarkMode = bottomMenu.isAutoTintIconInLightOrDarkMode();
+
+                    if (resId != 0) {
+                        viewHolder.imgDialogxMenuIcon.setVisibility(View.VISIBLE);
+                        viewHolder.imgDialogxMenuIcon.setImageResource(resId);
+                        if (viewHolder.spaceDialogxRightPadding != null) {
+                            viewHolder.spaceDialogxRightPadding.setVisibility(View.VISIBLE);
+                        }
+                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+                            if (autoTintIconInLightOrDarkMode) {
+                                viewHolder.imgDialogxMenuIcon.setImageTintList(ColorStateList.valueOf(context.getResources().getColor(textColor)));
+                            }
+                        }
+                    } else {
+                        viewHolder.imgDialogxMenuIcon.setVisibility(View.GONE);
+                        if (viewHolder.spaceDialogxRightPadding != null) {
+                            viewHolder.spaceDialogxRightPadding.setVisibility(View.GONE);
+                        }
+                    }
+                } else {
+                    viewHolder.imgDialogxMenuIcon.setVisibility(View.GONE);
+                    if (viewHolder.spaceDialogxRightPadding != null) {
+                        viewHolder.spaceDialogxRightPadding.setVisibility(View.GONE);
+                    }
                 }
             }
         }
@@ -226,7 +249,7 @@ public class BottomMenuArrayAdapter extends BaseAdapter {
         }
         return convertView;
     }
-    
+
     private int px2dip(float pxValue) {
         final float scale = context.getResources().getDisplayMetrics().density;
         return (int) (pxValue / scale + 0.5f);

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

@@ -120,8 +120,8 @@ public class PopMenuArrayAdapter extends BaseAdapter {
         
         if (popMenu.getOnIconChangeCallBack() != null) {
             int resId = popMenu.getOnIconChangeCallBack().getIcon(popMenu, position, menuList.get(position).toString());
-            boolean autoTintIconInLightOrDarkMode = popMenu.getOnIconChangeCallBack().isAutoTintIconInLightOrDarkMode();
-            
+            boolean autoTintIconInLightOrDarkMode = popMenu.getOnIconChangeCallBack().isAutoTintIconInLightOrDarkMode() == null ? popMenu.isAutoTintIconInLightOrDarkMode() : popMenu.getOnIconChangeCallBack().isAutoTintIconInLightOrDarkMode();
+
             if (resId != 0) {
                 viewHolder.imgDialogxMenuIcon.setVisibility(View.VISIBLE);
                 viewHolder.imgDialogxMenuIcon.setImageResource(resId);
@@ -142,9 +142,32 @@ public class PopMenuArrayAdapter extends BaseAdapter {
                 }
             }
         } else {
-            viewHolder.imgDialogxMenuIcon.setVisibility(View.GONE);
-            if (viewHolder.spaceDialogxRightPadding != null) {
-                viewHolder.spaceDialogxRightPadding.setVisibility(View.GONE);
+            if (popMenu.getIconResIds() != null) {
+                int resId = popMenu.getIconResIds(position);
+                boolean autoTintIconInLightOrDarkMode = popMenu.isAutoTintIconInLightOrDarkMode();
+
+                if (resId != 0) {
+                    viewHolder.imgDialogxMenuIcon.setVisibility(View.VISIBLE);
+                    viewHolder.imgDialogxMenuIcon.setImageResource(resId);
+                    if (viewHolder.spaceDialogxRightPadding != null) {
+                        viewHolder.spaceDialogxRightPadding.setVisibility(View.VISIBLE);
+                    }
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+                        if (autoTintIconInLightOrDarkMode) {
+                            viewHolder.imgDialogxMenuIcon.setImageTintList(ColorStateList.valueOf(context.getResources().getColor(textColor)));
+                        }
+                    }
+                } else {
+                    viewHolder.imgDialogxMenuIcon.setVisibility(View.GONE);
+                    if (viewHolder.spaceDialogxRightPadding != null) {
+                        viewHolder.spaceDialogxRightPadding.setVisibility(View.GONE);
+                    }
+                }
+            } else {
+                viewHolder.imgDialogxMenuIcon.setVisibility(View.GONE);
+                if (viewHolder.spaceDialogxRightPadding != null) {
+                    viewHolder.spaceDialogxRightPadding.setVisibility(View.GONE);
+                }
             }
         }
         

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

@@ -437,7 +437,8 @@ public class MainActivity extends BaseActivity {
         btnContextMenu.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
-                PopMenu.show(new String[]{"添加", "编辑", "删除", "分享"})
+                PopMenu.show("添加", "编辑", "删除", "分享")
+                        .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
                             public boolean onClick(PopMenu dialog, CharSequence text, int index) {
@@ -763,6 +764,10 @@ public class MainActivity extends BaseActivity {
                                 }
                             })
                             .show();
+
+//                      测试用代码
+//                    BottomMenu.show("添加", "查看", "编辑")
+//                            .setIconResIds(R.mipmap.img_dialogx_demo_add, R.mipmap.img_dialogx_demo_view, R.mipmap.img_dialogx_demo_edit);
                 } else {
                     BottomMenu.show(new String[]{"新标签页中打开", "稍后阅读", "复制链接网址"})
                             .setMessage("http://www.kongzue.com/DialogX")