Skip to content

Commit fed4dac

Browse files
committed
feat(menu): 支持按分组展示菜单
- 新增分组菜单功能,根据菜单的 groupName 进行分组 - 如果所有菜单都没有分组名称,则直接使用原来的方式显示菜单 -优化了菜单展示逻辑,提高了用户体验
1 parent 2bc9b9c commit fed4dac

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

menu/src/main/java/com/noober/menu/MenuUtil.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.noober.menu;
22

3+
import android.text.TextUtils;
34
import android.view.View;
45

56
import com.noober.menu.group.GroupedFloatMenu;
@@ -19,14 +20,39 @@ public class MenuUtil {
1920
public static <T> void showMenu(View targetView, List<IMenu<T>> menus, T info) {
2021
// 按 groupName 分组
2122
Map<String, List<IMenu<T>>> groupedMenus = new HashMap<>();
23+
boolean allEmpty = true;
2224
for (IMenu<T> menu : menus) {
2325
String groupName = menu.groupName(); // 假设 IMenu 接口新增了 groupName 方法
26+
if(TextUtils.isEmpty(groupName)){
27+
groupName = "默认分组";
28+
}else {
29+
allEmpty = false;
30+
}
2431
if (!groupedMenus.containsKey(groupName)) {
2532
groupedMenus.put(groupName, new ArrayList<>());
2633
}
2734
groupedMenus.get(groupName).add(menu);
2835
}
2936

37+
if(allEmpty){
38+
// 如果所有菜单都没有分组名称,则直接使用原来的方式显示菜单
39+
String[] desc = new String[menus.size()];
40+
for (int i = 0; i < menus.size(); i++) {
41+
desc[i] = menus.get(i).text();
42+
}
43+
FloatMenu floatMenu = new FloatMenu(targetView.getContext(), targetView);
44+
floatMenu.items(desc);
45+
floatMenu.setOnItemClickListener(new FloatMenu.OnItemClickListener() {
46+
@Override
47+
public void onClick(View v, int position) {
48+
menus.get(position).onMenuClicked(position, info);
49+
}
50+
});
51+
floatMenu.showAsDropDown(targetView);
52+
return;
53+
}
54+
55+
3056
// 构造 GroupedFloatMenu 所需的数据结构
3157
List<GroupedFloatMenu.MenuGroup> menuGroups = new ArrayList<>();
3258
for (Map.Entry<String, List<IMenu<T>>> entry : groupedMenus.entrySet()) {

0 commit comments

Comments
 (0)