11package de .dlyt .yanndroid .oneui .menu ;
22
33import android .content .Context ;
4+ import android .graphics .Typeface ;
5+ import android .text .TextUtils ;
46import android .util .TypedValue ;
57import android .view .KeyEvent ;
68import android .view .MotionEvent ;
79import android .view .View ;
810import android .view .ViewGroup ;
911import android .widget .ArrayAdapter ;
12+ import android .widget .LinearLayout ;
1013import android .widget .PopupWindow ;
14+ import android .widget .TextView ;
1115
1216import androidx .annotation .MenuRes ;
1317
@@ -28,6 +32,9 @@ public class PopupMenu {
2832 private PopupListView listView ;
2933 private PopupMenuAdapter popupMenuAdapter ;
3034
35+ private LinearLayout layoutWithTitle ;
36+ private CharSequence title ;
37+
3138 private int xoff = 0 ;
3239 private int yoff = 0 ;
3340
@@ -54,8 +61,17 @@ public void inflate(@MenuRes int menuRes) {
5461 inflate (new Menu (menuRes , context ));
5562 }
5663
64+ public void inflate (@ MenuRes int menuRes , CharSequence title ) {
65+ inflate (new Menu (menuRes , context ), title );
66+ }
67+
5768 public void inflate (Menu menu ) {
69+ inflate (menu , null );
70+ }
71+
72+ public void inflate (Menu menu , CharSequence title ) {
5873 this .menu = menu ;
74+ this .title = title ;
5975
6076 if (popupWindow != null ) {
6177 if (popupWindow .isShowing ()) {
@@ -78,8 +94,8 @@ public void inflate(Menu menu) {
7894 MenuItem clickedMenuItem = menu .getItem (position );
7995 if (clickedMenuItem .isCheckable ()) clickedMenuItem .toggleChecked ();
8096 if (clickedMenuItem .hasSubMenu ()) {
81- PopupMenu subPopupMenu = new PopupMenu (anchor ); //itemViews.get(position)
82- subPopupMenu .inflate (clickedMenuItem .getSubMenu ());
97+ PopupMenu subPopupMenu = new PopupMenu (anchor );
98+ subPopupMenu .inflate (clickedMenuItem .getSubMenu (), menu . getItem ( position ). getTitle () );
8399 subPopupMenu .setPopupMenuListener (new PopupMenuListener () {
84100 @ Override
85101 public boolean onMenuItemClick (MenuItem item ) {
@@ -98,12 +114,18 @@ public void onMenuItemUpdate(MenuItem menuItem) {
98114 }
99115 });
100116
101- listView .measure (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
102- int height = listView .getMeasuredHeight () + context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_bottom_padding ) - 5 ;
117+ if (title != null ) {
118+ layoutWithTitle = new LinearLayout (context );
119+ layoutWithTitle .setOrientation (LinearLayout .VERTICAL );
120+ layoutWithTitle .addView (createTitleView ());
121+ layoutWithTitle .addView (listView );
122+ popupWindow = new PopupWindow (layoutWithTitle );
123+ } else {
124+ popupWindow = new PopupWindow (listView );
125+ }
103126
104- popupWindow = new PopupWindow (listView );
105127 popupWindow .setWidth (getPopupMenuWidth ());
106- popupWindow .setHeight (height );
128+ popupWindow .setHeight (getPopupMenuHeight () );
107129 popupWindow .setAnimationStyle (R .style .MenuPopupAnimStyle );
108130 popupWindow .setBackgroundDrawable (context .getResources ().getDrawable (R .drawable .sesl_menu_popup_background , context .getTheme ()));
109131 popupWindow .setOutsideTouchable (true );
@@ -129,6 +151,22 @@ public boolean onTouch(View view, MotionEvent motionEvent) {
129151 });
130152 }
131153
154+ private TextView createTitleView () {
155+ TextView titleView = new TextView (context );
156+ titleView .setPadding (context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_start_padding ),
157+ context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_top_padding ),
158+ context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_end_padding ),
159+ context .getResources ().getDimensionPixelSize (R .dimen .sesl_menu_popup_bottom_padding ));
160+ titleView .setTextDirection (View .TEXT_DIRECTION_LOCALE );
161+ titleView .setTextColor (context .getResources ().getColor (R .color .item_color ));
162+ titleView .setTypeface (Typeface .DEFAULT_BOLD );
163+ titleView .setEllipsize (TextUtils .TruncateAt .END );
164+ titleView .setMaxLines (1 );
165+ titleView .setTextSize (16 );
166+ titleView .setText (title );
167+ return titleView ;
168+ }
169+
132170 public int getPopupMenuWidth () {
133171 int makeMeasureSpec = View .MeasureSpec .makeMeasureSpec (0 , View .MeasureSpec .UNSPECIFIED );
134172 int popupWidth = 0 ;
@@ -144,10 +182,18 @@ public int getPopupMenuWidth() {
144182 return popupWidth ;
145183 }
146184
185+ public int getPopupMenuHeight () {
186+ if (title == null ) {
187+ listView .measure (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
188+ return listView .getMeasuredHeight () + context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_bottom_padding ) - 5 ;
189+ } else {
190+ layoutWithTitle .measure (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
191+ return layoutWithTitle .getMeasuredHeight () + context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_bottom_padding ) - 5 ;
192+ }
193+ }
194+
147195 private void updatePopupSize () {
148- listView .measure (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT );
149- int height = listView .getMeasuredHeight () + context .getResources ().getDimensionPixelSize (R .dimen .sesl_popup_menu_item_bottom_padding ) - 5 ;
150- if (popupWindow .isShowing ()) popupWindow .update (getPopupMenuWidth (), height );
196+ if (popupWindow .isShowing ()) popupWindow .update (getPopupMenuWidth (), getPopupMenuHeight ());
151197 }
152198
153199 public void show () {
0 commit comments