1
1
package com .noober .menu ;
2
2
3
+ import android .text .TextUtils ;
3
4
import android .view .View ;
4
5
5
6
import com .noober .menu .group .GroupedFloatMenu ;
@@ -19,14 +20,39 @@ public class MenuUtil {
19
20
public static <T > void showMenu (View targetView , List <IMenu <T >> menus , T info ) {
20
21
// 按 groupName 分组
21
22
Map <String , List <IMenu <T >>> groupedMenus = new HashMap <>();
23
+ boolean allEmpty = true ;
22
24
for (IMenu <T > menu : menus ) {
23
25
String groupName = menu .groupName (); // 假设 IMenu 接口新增了 groupName 方法
26
+ if (TextUtils .isEmpty (groupName )){
27
+ groupName = "默认分组" ;
28
+ }else {
29
+ allEmpty = false ;
30
+ }
24
31
if (!groupedMenus .containsKey (groupName )) {
25
32
groupedMenus .put (groupName , new ArrayList <>());
26
33
}
27
34
groupedMenus .get (groupName ).add (menu );
28
35
}
29
36
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
+
30
56
// 构造 GroupedFloatMenu 所需的数据结构
31
57
List <GroupedFloatMenu .MenuGroup > menuGroups = new ArrayList <>();
32
58
for (Map .Entry <String , List <IMenu <T >>> entry : groupedMenus .entrySet ()) {
0 commit comments